Linq2Xml 解析 REST Web 服务的输出

发布于 2024-10-09 23:39:21 字数 1871 浏览 2 评论 0原文

我正在尝试解析来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2024-10-16 23:39:21

我可以看到您的代码片段存在三个问题。首先,

    Dim query = From c In loaded.<Response> Select c

您不需要

    Dim query = From c In loaded.<Response>.Elements Select c

甚至只是

    Dim query = loaded.<Response>.Elements

获取项目而不是响应。

另外,您需要添加

    dt.Rows.Add(dr)

到循环中,否则新行将不会添加到数据表中。

最后,XML 区分大小写,因此您需要使用 @Name,而不是 @name

There are three problems I can see with your code snippet. First, instead of

    Dim query = From c In loaded.<Response> Select c

you need

    Dim query = From c In loaded.<Response>.Elements Select c

or even just

    Dim query = loaded.<Response>.Elements

to fetch the Items rather than the Response.

Also, you need to add

    dt.Rows.Add(dr)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文