【问题标题】:How do I Parse this XML我如何解析这个 XML
【发布时间】:2014-05-01 03:06:15
【问题描述】:

我需要使用 Visual Basic .NET 解析下面的 XML。过去,我通过 XSLT 找到了一种解决方法,使 XML 适应我的需要,但我非常想知道如何在没有解决方法的情况下做到这一点。我会先粘贴 XML,然后我会告诉你我卡在哪里以及为什么卡住:

<browse result="1" first="1" last="16" total="16">
    <th>
        <td label="dimension" hideforuser="false" type="String">fin.trs.line.dim2</td>
        <td label="Outstanding" hideforuser="false" type="Value">fin.trs.line.openbasevaluesigned</td>
        <td label="Factuurbedrag" hideforuser="false" type="Value">fin.trs.line.basevaluesigned</td>
        <td label="Invoice Number" hideforuser="false" type="String">fin.trs.line.invnumber</td>
        <td label="" hideforuser="false" type="String">fin.trs.head.code</td>
        <td label="Pay date" hideforuser="false" type="Date">fin.trs.line.matchdate</td>
        <td label="Vervaldatum" hideforuser="false" type="Date">fin.trs.line.datedue</td>
        <td label="Datum" hideforuser="false" type="Date">fin.trs.head.date</td>
        <td label="boektype" hideforuser="false" type="String">fin.trs.head.status</td>
        <td label="paystatus" hideforuser="false" type="String">fin.trs.line.availableforpayruns</td>
    </th>
    <tr>
        <td field="fin.trs.line.dim2" hideforuser="false" type="String">01603</td>
        <td field="fin.trs.line.openbasevaluesigned" hideforuser="false" type="Value">-792.00</td>
        <td field="fin.trs.line.basevaluesigned" hideforuser="false" type="Value">-800.00</td>
        <td field="fin.trs.line.invnumber" hideforuser="false" type="String">789</td>
        <td field="fin.trs.head.code" hideforuser="false" type="String">INK</td>
        <td field="fin.trs.line.matchdate" hideforuser="false" type="Date" name="14/03/2012">20120314</td>
        <td field="fin.trs.line.datedue" hideforuser="false" type="Date" name="13/04/2012">20120413</td>
        <td field="fin.trs.head.date" hideforuser="false" type="Date" name="14/03/2012">20120314</td>
        <td field="fin.trs.head.status" hideforuser="false" type="String" name="Definitief">final</td>
        <td field="fin.trs.line.availableforpayruns" hideforuser="false" type="String" name="Ja">true</td>
        <key>
            <office>DACMI3-1</office>
            <code>INK</code>
            <number>201200019</number>
            <line>1</line>
        </key>
    </tr>
<tr>
        <td field="fin.trs.line.dim2" hideforuser="false" type="String">11123</td>
        <td field="fin.trs.line.openbasevaluesigned" hideforuser="false" type="Value">300.00</td>
        <td field="fin.trs.line.basevaluesigned" hideforuser="false" type="Value">300.00</td>
        <td field="fin.trs.line.invnumber" hideforuser="false" type="String">11112</td>
        <td field="fin.trs.head.code" hideforuser="false" type="String">INK</td>
        <td field="fin.trs.line.matchdate" hideforuser="false" type="Date"/>
        <td field="fin.trs.line.datedue" hideforuser="false" type="Date" name="13/04/2012">20120413</td>
        <td field="fin.trs.head.date" hideforuser="false" type="Date" name="14/03/2012">20120314</td>
        <td field="fin.trs.head.status" hideforuser="false" type="String" name="Definitief">final</td>
        <td field="fin.trs.line.availableforpayruns" hideforuser="false" type="String" name="Ja">true</td>
        <key>
            <office>DACMI3-1</office>
            <code>INK</code>
            <number>201200021</number>
            <line>1</line>
        </key>
    </tr>
    </browse>

为了让所有人都能阅读,我截断了 XML。实际 XML 中还有大约 15 个 &lt;tr&gt; 部分。 XML 是我从网络服务获得的响应。

我已经尝试了可以​​在网络上找到的所有代码,但我被困在了每个代码上。这就是为什么我不会向你展示我已经尝试过的东西,请相信我已经花了两年时间。

我的问题是什么

1。如您所见,XML 以 &lt;th&gt; 部分开头(这对我来说并不重要,这只是 web 服务告诉我我的要求)。所以这部分需要忽略。但它与我确实需要的&lt;tr&gt; 块处于同一“级别”,那么我如何跳过&lt;th&gt; 并从&lt;tr&gt; 开始?就像for each tr 一样。

2。我需要 &lt;tr&gt; 块中的每个值和名称,但是 names 不是由标签指定的,而是由属性指定的。如果您查看&lt;tr&gt; 块;而不是

&lt;dim2&gt;01603&lt;/dim2&gt;

它被记录为

<td field="fin.trs.line.dim2" hideforuser="false" type="String">01603</td>

对于每个&lt;tr&gt; 块,我需要fin.trs.line.dim2 部分(即字段名称)和实际值。那我该怎么做呢?

3。每个&lt;tr&gt; 块都有一个名为&lt;key&gt; 的子节点,它(就像它所说的那样)保存每个块的键值。如何检索这些值,并确保我知道它们属于它所在的 &lt;tr&gt; 块?

我一直在阅读教程和网站,但我似乎无法理解这部分。

只是为了确保,这是针对 Visual Basic NET(2010,如果您需要知道的话)。

P.S.:XML 在一个字符串中。

【问题讨论】:

    标签: .net xml vb.net xml-parsing linq-to-xml


    【解决方案1】:

    您可以使用LINQ2XML 仅获取tr 元素,然后使用XDocument.Descendantsth 元素中提取属性XAttribute 方法。要查找 key 元素,请再次对 tr 元素使用 Descendants 方法。解决方案可能如下所示:

    dim xmlSource = File.ReadAllText("d:\temp\source.xml")
    ' read the XML (HTML) code
    dim xml = XDocument.Parse(xmlSource)
    ' find all tr elements
    dim trs = xml.Root.Descendants("tr").ToList()
    ' iterate over each one of them
    for Each tr in trs
        ' find all td elements for each tr
        dim tds = tr.Descendants("td")
        ' iterate over each one of them
        for each td as XElement in tds
            ' find the attribute with the name field
            dim attr as XAttribute = td.Attribute("field")
            ' if found
            if not (attr.Value = nothing) then
                ' take the attribute name and the element value (td value)
                Console.WriteLine(String.Format("{0} - {1}", attr.Value, td.Value))
            end if
        next
        ' find element key that belongs to this tr
        dim keys = tr.Descendants("key")
        for each key as XElement in keys
            dim keyNodes = key.Elements()
            for each keyNode as XElement in keyNodes
                Console.WriteLine(String.Format("{0} - {1}", keyNode.Name, keyNode.Value))
            next
        next
    next
    

    输出是(只是其中的一部分):

    ...
    fin.trs.head.date - 20120314
    fin.trs.head.status - final
    fin.trs.line.availableforpayruns - true
    office - DACMI3-1
    code - INK
    number - 201200021
    line - 1
    

    【讨论】:

    • 这正是我需要的Pasty。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2013-06-07
    • 2013-06-09
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多