【问题标题】:VB.NET Loop Through XML ResultsVB.NET 循环通过 XML 结果
【发布时间】:2016-01-18 15:17:20
【问题描述】:

我已经重做这个问题,因为一些人认为我很难理解我所追求的,所以稍微减少了这个问题。

<?xml version="1.0"?>
<root>
    <succesfulResponses>
        <position>0</position>
        <response>
            <dln>BBUTU204250VS9VT</dln>
            <licence>
                <entitlements>
                    <code>A</code>
                    <validFrom/>
                    <validTo/>
                    <priorTo>false</priorTo>
                    <type>F</type>
                </entitlements>
                <entitlements>
                    <code>B</code>
                    <validFrom/>
                    <validTo/>
                    <priorTo>false</priorTo>
                    <type>F</type>
                </entitlements>
            </licence>
        </response>
    </succesfulResponses>
    <succesfulResponses>
        <position>1</position>
        <response>
            <dln>BTXRS755313Y99AT</dln>
            <licence>           
                <entitlements>
                    <code>A</code>
                    <validFrom>2003-02-28</validFrom>
                    <validTo>2043-05-30</validTo>
                    <priorTo>false</priorTo>
                    <type>P</type>
                </entitlements>
                <entitlements>
                    <code>AM</code>
                    <validFrom>2014-05-14</validFrom>
                    <validTo>2043-05-30</validTo>
                    <priorTo>false</priorTo>
                    <type>P</type>
                </entitlements>
            </licence>
            <httpStatusCode>200</httpStatusCode>
        </response>
    </succesfulResponses>
</root>

这是我返回的 XML,我向服务提交了几个 ID,它返回了这个 XML。

如果我发送 2 个 ID,它会为每个 ID 返回 2 个“successfulResponses”元素,您可以在“response”下方的子节点“dln”中看到这些 ID,您可以看到它们是不同的。

'position' 就是我首先在'request' 中提交的ID。 'response' 也有一个叫做 'licence' 的元素和几个 'entitlement' 元素。

我希望将这些“权利”插入以这种方式格式化的数据表中

    Results.EntitlementsTbl.Columns.Add(New DataColumn("Code", GetType(String)))
    Results.EntitlementsTbl.Columns.Add(New DataColumn("Valid From", GetType(String)))
    Results.EntitlementsTbl.Columns.Add(New DataColumn("Valid To", GetType(String)))
    Results.EntitlementsTbl.Columns.Add(New DataColumn("Prior To", GetType(String)))
    Results.EntitlementsTbl.Columns.Add(New DataColumn("Type", GetType(String)))
    Results.EntitlementsTbl.Columns.Add(New DataColumn("Driver", GetType(String)))

每一列都与每个“权利”元素下方的一个节点相关。每当我有一个“权利”元素时,我都希望在我的数据表中添加一个新行。

还有一个额外的列“驱动程序”,需要用“位置”中的数字填充。

所以我的表的输出我想要以下内容:

如您所见,“驱动程序”列与“位置”节点的值相关。

我希望这比我以前更有意义。我已经删除了我自己声明的任何代码,因为它显然不是必需的。

【问题讨论】:

  • 您是否考虑过将您的 JSON 反序列化为 .net 对象?转换为 Xml 并进行查询似乎需要很多开销。
  • @Filburt 恐怕我真的不知道从哪里开始我所知道的一切都是自学的,所以这对我来说都是一个学习曲线。跨度>
  • 查看VB.net JSON Deserialize - 它应该可以帮助您入门。
  • @Filburt 谢谢你,理想情况下,当我有更多时间时,我会想办法解决这个问题。我现在只需要将这些数据放入表格中。
  • 这里不完全清楚你想要实现什么输出 - 你能从你的 XML 文档中添加预期的输出吗?你只接受了最后一个dln,但是使用所有司机的职位,你想要什么权利,你想要他们在哪里?

标签: xml vb.net


【解决方案1】:

我非常赞同将 JSON 直接反序列化为 CLR 对象的建议,您可能还会发现使用 XDocument API 更容易,但如果您出于某种原因确实必须使用 XmlDocument 模型,这段代码应该会有所帮助

Dim DriverNo As String

Using nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/root/succesfulResponses")
    For Each node In nodes 
        Dim DLN As String = ""
        DriverNo = node.SelectSingleNode("position").InnerText
        DLN = node.SelectSingleNode("response/dln").InnerText.ToString()

        Using entitlements As XmlNodeList = node.SelectNodes("response/licence/entitlements")
            For Each entitlement In entitlements
                Dim code, validFrom, validTo, priorTo, type As String
                code = entitlement.SelectSingleNode("code").InnerText
                validFrom = entitlement.SelectSingleNode("validFrom").InnerText
                validTo = entitlement.SelectSingleNode("validTo").InnerText
                priorTo = entitlement.SelectSingleNode("priorTo").InnerText
                type = entitlement.SelectSingleNode("type").InnerText
                ' do what you need to with the variables here
            Next

        End Using

    Next
End Using

这样,您将遍历每个 successfulResponses 节点,获取 DLN 和 DriverNo,然后遍历每个 entitlements 节点并从那里获取数据。这将导致每个权利一行。

do what you need to with the variables here 将是您插入数据表或您正在使用的任何内容的实际代码。

【讨论】:

  • 嗨,Dan,已经整理好我需要的东西的顶级人物,我一定会研究 JSON 到 CLR 对象,但这是一个新的学习曲线,我没有时间做正确的事情现在。
【解决方案2】:

试试 xml Linq

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()

        Dim doc As XDocument = XDocument.Load(FILENAME)

        Dim results = doc.Descendants("succesfulResponses").Select(Function(x) New With { _
           .position = CType(x.Element("position"), Integer), _
           .entitlements = x.Descendants("entitlements").Select(Function(y) New With { _
                               .code = CType(y.Element("code"), String), _
                               .validFrom = y.Elements("validFrom").Where(Function(z) z.Value.Length > 0).Select(Function(a) CType(a, DateTime)).FirstOrDefault(), _
                               .validTo = y.Elements("validTo").Where(Function(z) z.Value.Length > 0).Select(Function(a) CType(a, DateTime)).FirstOrDefault(), _
                               .priorTo = y.Elements("priorTo").Where(Function(z) z.Value.Length > 0).Select(Function(a) CType(a, Boolean)).FirstOrDefault(), _
                               .type = y.Elements("type").Where(Function(z) z.Value.Length > 0).Select(Function(a) CType(a, String)).FirstOrDefault() _
                               }).ToList() _
        }).ToList()

    End Sub

End Module

【讨论】:

  • 调试的噩梦。
  • 代码有什么问题。它一直在使用,尤其是在 c# 中。代码有效,那么有什么要调试的?
猜你喜欢
  • 1970-01-01
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多