English 中文(简体)
Parse REST XML
原标题:Parse REST XML file

我正在开发一个NET 3.5Windows 将与教育、科学和技术部网络服务处互动,该服务处将XML文件投入使用,例如:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Response Status="OK">
<Item Name="NumberZones">2</Item>
<Item Name="CurrentZoneID">10001</Item>
<Item Name="CurrentZoneIndex">1</Item>
<Item Name="ZoneName0">Westralia</Item>
<Item Name="ZoneID0">0</Item>
<Item Name="ZoneGUID0">{81C56183-31DA-45C2-90C3-81609F01B38B}</Item>
<Item Name="ZoneName1">Lounge</Item>
<Item Name="ZoneID1">10001</Item>
<Item Name="ZoneGUID1">{eac0109e-0090-a992-7fba-dc67fe29e6e7}</Item>
</Response>

我需要帮助的是,我如何把这一档案归结起来,以便我能够获得项目名称(例如Name0区)的价值和属性价值(就本案而言,西里利亚)。

我一直在利用XDocument来复制XML文件,并且能够这样做,但我认为,这样做极为缓慢,因为我正在通过XML文件,试图与该项目的名称匹配:

Public Function getZoneDetails(ByVal serverName As String, ByVal serverPort As String) As DataTable
        Dim sResult As DataTable
        sResult = New DataTable("ZoneDetails")
        With sResult
            .Columns.Add("column")
            .Columns.Add("data")
            .Columns.Add("zoneID")
            .Columns.Add("result")
        End With
        xmlDocPath = "temp.xml"
        If Not DownloadXml(serverName, serverPort, "Playback/Zones", xmlDocPath) Then
            Dim fDtRow As DataRow = sResult.NewRow()
            fDtRow.Item(0) = ""
            fDtRow.Item(1) = ""
            fDtRow.Item(2) = ""
            fDtRow.Item(3) = "Error downloading XML"
            sResult.Rows.Add(fDtRow)
            Return sResult
        End If
        If Not LoadXml(xmlDocPath) Then
            Dim fDtRow As DataRow = sResult.NewRow()
            fDtRow.Item(0) = ""
            fDtRow.Item(1) = ""
            fDtRow.Item(2) = ""
            fDtRow.Item(3) = "Error loading XML internally"
            sResult.Rows.Add(fDtRow)
            Return sResult
        End If
        Try
              Make sure <status> = OK
            Dim status As String = xdoc.Root.Attribute("Status").Value
            If status <> "OK" Then
                Dim fDtRow As DataRow = sResult.NewRow()
                fDtRow.Item(0) = ""
                fDtRow.Item(1) = ""
                fDtRow.Item(2) = ""
                fDtRow.Item(3) = "No route can be mapped out"
                sResult.Rows.Add(fDtRow)
                Return sResult
            End If

            Dim zoneID As String = String.Empty
            Dim i As Integer = 0
            For Each obj In xdoc.Root.Elements("Item").Attributes("Name")
                Dim nRow As DataRow = sResult.NewRow()
                If obj.Value = "ZoneID" + i.ToString Then
                    zoneID = obj.Parent.Value
                    nRow.Item(0) = "ZoneID"
                    nRow.Item(1) = zoneID
                    nRow.Item(2) = zoneID
                    nRow.Item(3) = "OK"
                End If

                If obj.Value = "ZoneName" + i.ToString Then
                    nRow.Item(0) = "ZoneName"
                    nRow.Item(1) = obj.Parent.Value
                    nRow.Item(2) = zoneID
                    nRow.Item(3) = "OK"
                End If

                If obj.Value = "ZoneGUID" + i.ToString Then
                    nRow.Item(0) = "ZoneGUID"
                    nRow.Item(1) = obj.Parent.Value
                    nRow.Item(2) = zoneID
                    nRow.Item(3) = "OK"
                End If
                i = i + 1
                sResult.Rows.Add(nRow)
            Next
            Return sResult
        Catch ex As Exception
            Dim fDtRow As DataRow = sResult.NewRow()
            fDtRow.Item(0) = ""
            fDtRow.Item(1) = ""
            fDtRow.Item(2) = ""
            fDtRow.Item(3) = "Error occured"
            sResult.Rows.Add(fDtRow)
            Return sResult
        End Try
    End Function

我想读一下XML,将属性和属性名称归入一个数据集,受下级名单的约束。

如果有一个更好的方法,我想要达到什么目标,谁能告诉大家? 我是在找到最佳解决办法之后。

如果存在任何问题,请主动犹豫,让我知道。

最佳回答

如果你想改进业绩,我会考虑几个方面:

1- 停止将XML下载到磁盘上的一个档案中,然后将档案重新输入XML。 利用HttpWebRequest或HttpClient,你可以检索数据,作为溪流,然后将数据直接输入XElement/XDocument物体。

2- 不装入数据表一,将使用Linq2Xml查询文件并收集物体。

问题回答

In the past, I ve successfully used Json.NET to parse the result from calls to Google Translation API or the Twitter API.





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签