代码之家  ›  专栏  ›  技术社区  ›  Rene Gijzemijter

如何解析此XML

  •  0
  • Rene Gijzemijter  · 技术社区  · 10 年前

    我需要使用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。还有大约15个 <tr> 实际XML中的节。XML是我从Web服务得到的响应。

    我试过在网上能找到的每一段代码,但我都陷入了困境。这就是为什么我不会向你展示我已经尝试过的东西,请相信我已经花了两年的时间。

    我的问题是什么 :

    1. 。如您所见,XML以 <th> 部分(这对我来说并不重要,只是网络服务告诉我我需要什么)。因此,这一部分需要被忽略。但它与 <tr> 我确实需要的块,那么我如何跳过 <第> 一个,从 <tr> 一个?如在 for each tr 事情

    2. 。我需要 <tr> 块,但 名称 不是由标记指定的,而是由属性指定的。如果你看 <tr> 阻碍;而不是

    <dim2>01603</dim2>

    它被记为

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

    每个 <tr> 街区我需要 fin.trs.line.dim2 part(因此字段名)和实际值。那么我该怎么做呢?

    3. 每个 <tr> 块有一个名为的子节点 <key> ,它(就像告诉的那样)保存每个块的键值。如何检索这些值,并确保它们属于 <tr> 阻止它在里面?

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

    为了确保,这是针对Visual Basic NET(如果您需要了解,请参见2010)的。

    备注:XML是字符串。

    1 回复  |  直到 10 年前
        1
  •  0
  •   keenthinker    10 年前

    您可以使用 LINQ2XML 去拿 tr 元素,然后提取 属性 以及 价值 来自 th 元素 XDocument.Descendants XAttribute 方法要查找 钥匙 元素使用 后代 方法 第三方 要素解决方案可能如下:

    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