![](/img/trans.png)
[英]How do I create an XML document with an <!ENTITY> and a <!NOTATION> that XmlDocument.Load() can parse
[英]How do I Parse this XML
我需要使用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是我從Web服務獲得的響應。
我已經嘗試過可以在網上找到的每段代碼,但是卻被困在每一個代碼上。 這就是為什么我不向您展示我已經嘗試過的東西的原因,請相信我已經花了兩年的時間。
我的問題是什么 :
1 。 如您所見,XML從<th>
部分開始(這對我來說並不重要,只是Web服務告訴我所要的內容)。 因此,該部分需要忽略。 但它是在同一個“級別”為<tr>
我確實需要的塊,讓我怎么跳過<th>
之一,在啟動<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
部分(因此是字段名)和實際值。 那我該怎么做呢?
3 。 每個<tr>
塊都有一個名為<key>
的子節點,正如其所告訴的那樣,該子節點保存每個塊的鍵值。 如何獲取這些值,並確保我知道它們屬於它所在的<tr>
塊?
我一直在閱讀教程和網站,但是我似乎根本無法理解這一部分。
只是為了確保這適用於Visual Basic NET(2010,如果需要了解)。
PS:XML在字符串中。
您可以使用LINQ2XML僅獲取tr
元素,然后使用XDocument.Descendants和XAttribute方法從th
元素提取屬性和值 。 要查找關鍵元素,請再次在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.