Linq2Xml 解析 REST Web 服务的输出
我正在尝试解析来自 REST Web 服务的 XML 输出,并且已指出使用 Linq2Xml 来查询 XML 中我所需要的属性的方向。 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>
我想要在数据表中返回 ZoneID、ZoneName 和 ZoneGUID,我想要函数返回类似以下内容:
id name guid
0 westralia {81C56183-31DA-45C2-90C3-81609F01B38B}
10001 lounge {eac0109e-0090-a992-7fba-dc67fe29e6e7}
我一直在使用以下函数来查询 XML,并得到了如下结果:就尝试返回结果而言(甚至在尝试操作数据以将其转换为我想要的格式时也没有)。
Private Function getServerResponse_Linq(ByVal queryString As Uri) As DataTable
Dim dt As DataTable = New DataTable("data")
With dt
.Columns.Add("name")
.Columns.Add("Value")
End With
Dim loaded As XDocument = XDocument.Load(queryString.ToString)
Dim dr As DataRow
Dim query = From c In loaded.<Response> Select c
For Each result In query
dr = dt.NewRow
With dr
.Item("name") = result.@name
.Item("value") = result.Value
End With
Next
Return dt
End Function
返回的数据表为空,我已确认 result 确实分配了 XML,并且 result.value 是 XML 文件的字符串版本 (200Westralia0{81C56183-31DA-45C2-90C3-81609F01B38B}Lounge10001{eac0109e-0090-a992- 7fba-dc67fe29e6e7})
有谁能够帮助我如何从上面的 REST XML 示例中读取名称 (ZoneName%) 和属性值(Westralia 和 Lounge)?
I am attempting to parse the XML output from a REST web service and have been pointed in the direction of using Linq2Xml to query the XML for the attributes that I am after. The XML output is as follows:
<?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>
I am wanting to return in a datatable the ZoneID, ZoneName, and ZoneGUID, I am wanting the function to return something like:
id name guid
0 westralia {81C56183-31DA-45C2-90C3-81609F01B38B}
10001 lounge {eac0109e-0090-a992-7fba-dc67fe29e6e7}
I have been working on the following function to query the XML, and have gotten as far as attempting to return results (not even at the point of trying to manipulate the data to get it into the format that I am wanting).
Private Function getServerResponse_Linq(ByVal queryString As Uri) As DataTable
Dim dt As DataTable = New DataTable("data")
With dt
.Columns.Add("name")
.Columns.Add("Value")
End With
Dim loaded As XDocument = XDocument.Load(queryString.ToString)
Dim dr As DataRow
Dim query = From c In loaded.<Response> Select c
For Each result In query
dr = dt.NewRow
With dr
.Item("name") = result.@name
.Item("value") = result.Value
End With
Next
Return dt
End Function
The returned datatable is empty, I have confirmed that result does have the XML assigned, and result.value is the string version of the XML file (200Westralia0{81C56183-31DA-45C2-90C3-81609F01B38B}Lounge10001{eac0109e-0090-a992-7fba-dc67fe29e6e7})
Is anyone able to provide assistance in how I can read from the REST XML example above the name (ZoneName%) and the attribute value (Westralia and Lounge)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以看到您的代码片段存在三个问题。首先,
您不需要
甚至只是
获取项目而不是响应。
另外,您需要添加
到循环中,否则新行将不会添加到数据表中。
最后,XML 区分大小写,因此您需要使用
@Name
,而不是@name
。There are three problems I can see with your code snippet. First, instead of
you need
or even just
to fetch the Items rather than the Response.
Also, you need to add
to your loop, otherwise your new row won't get added to the DataTable.
Finally, XML is case sensitive so you need to use
@Name
, not@name
.