【发布时间】: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 个 <tr> 部分。 XML 是我从网络服务获得的响应。
我已经尝试了可以在网络上找到的所有代码,但我被困在了每个代码上。这就是为什么我不会向你展示我已经尝试过的东西,请相信我已经花了两年时间。
我的问题是什么:
1。如您所见,XML 以 <th> 部分开头(这对我来说并不重要,这只是 web 服务告诉我我的要求)。所以这部分需要忽略。但它与我确实需要的<tr> 块处于同一“级别”,那么我如何跳过<th> 并从<tr> 开始?就像for each tr 一样。
2。我需要 <tr> 块中的每个值和名称,但是 names 不是由标签指定的,而是由属性指定的。如果您查看<tr> 块;而不是
<dim2>01603</dim2>
它被记录为
<td field="fin.trs.line.dim2" hideforuser="false" type="String">01603</td>
对于每个<tr> 块,我需要fin.trs.line.dim2 部分(即字段名称)和实际值。那我该怎么做呢?
3。每个<tr> 块都有一个名为<key> 的子节点,它(就像它所说的那样)保存每个块的键值。如何检索这些值,并确保我知道它们属于它所在的 <tr> 块?
我一直在阅读教程和网站,但我似乎无法理解这部分。
只是为了确保,这是针对 Visual Basic NET(2010,如果您需要知道的话)。
P.S.:XML 在一个字符串中。
【问题讨论】:
标签: .net xml vb.net xml-parsing linq-to-xml