我正在开发一个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,将属性和属性名称归入一个数据集,受下级名单的约束。
如果有一个更好的方法,我想要达到什么目标,谁能告诉大家? 我是在找到最佳解决办法之后。
如果存在任何问题,请主动犹豫,让我知道。