我试图用这种类型的布局解析这个XML文件:
<POX>
<LIST>
<COMPANY>
<A_COMPANY>The company here</A_COMPANY>
<A_ID>786</A_ID>
<A_BASE>USD</A_BASE>
<A_YES>Yes</A_YES>
<A_NO>No</A_NO>
<A_CATEGORY_D>1210043021</A_CATEGORY_D>
<A_PRECISION>2</A_PRECISION>
<LIST_BREAK>
<G_BREAK>
<CURRENCY>2</CURRENCY>
<SORT_COLUMN>100</SORT_COLUMN>
etc....
</G_BREAK>
<G_BREAK>
<CURRENCY>25</CURRENCY>
<SORT_COLUMN>130</SORT_COLUMN>
etc....
</G_BREAK>
<G_BREAK>
<CURRENCY>77</CURRENCY>
<SORT_COLUMN>1350</SORT_COLUMN>
etc....
</G_BREAK>
</LIST_BREAK>
ETC........
我似乎无法让这段代码适用于它:
Dim m_xmlr As XmlTextReader
m_xmlr = New XmlTextReader("c:\temp\46659024.xml")
m_xmlr.WhitespaceHandling = WhitespaceHandling.None
m_xmlr.Read()
m_xmlr.Read()
While Not m_xmlr.EOF
m_xmlr.Read()
If Not m_xmlr.IsStartElement() Then
Exit While
End If
Dim strTest1 = m_xmlr.GetAttribute("/POX/LIST/COMPANY/LIST_BREAK/G_BREAK")
m_xmlr.Read()
Dim strTest2 = m_xmlr.ReadElementString("CURRENCY")
Dim strTest3 = m_xmlr.ReadElementString("SORT_COLUMN")
Console.WriteLine("test1: " & strTest1 _
& " test2: " & strTest2 & " test3: " _
& strTest3)
Console.Write(vbCrLf)
Console.Read()
End While
我在
上收到错误 Dim **strTest2 = m_xmlr.ReadElementString("CURRENCY")**
话说:
Element 'CURRENCY' was not found. Line 4, position 4.
此外,strTest1返回无
我可能做错了什么?
谢谢!
更新 这是有效的,但它似乎没有得到所有的项目..
m_xmld = New XmlDocument()
m_xmld.Load("c:\temp\o1293688.xml")
m_nodelist = m_xmld.SelectNodes("/POXPOVPS/LIST_G_COMPANY/G_COMPANY/LIST_G_VENDORS_BREAK/G_VENDORS_BREAK")
Dim ListItem1 As ListViewItem
'Loop through the nodes
For Each m_node In m_nodelist
vendorName(x) = m_node.ChildNodes.Item(3).InnerText 'Name of Vendor
ListItem1 = ListView1.Items.Add(vendorName(x), 1)
x = x + 1
Next
x = 0
m_nodelist = m_xmld.SelectNodes("/POXPOVPS/LIST_G_COMPANY/G_COMPANY/LIST_G_VENDORS_BREAK/G_VENDORS_BREAK/LIST_G_SITE_1/G_SITE_1")
For Each m_node In m_nodelist
vendorAddress(x) = m_node.ChildNodes.Item(0).InnerText 'Address of Vendor
vendorTotal(x) = m_node.ChildNodes.Item(5).InnerText 'Total for vendor
ListView1.Items(0).SubItems.Add(vendorAddress(x))
ListView1.Items(0).SubItems.Add(vendorTotal(x))
x = x + 1
Next
vendorName共获得x = 1435 vendorAddress和vendorTotal得到的总数为x = 1481 .....将所有值放在一起时,这不会很好....
大卫
答案 0 :(得分:1)
你可以将它用于初学者..
' Load XML from somewhere...
Dim xml As XElement = XElement.Parse(GetXML())
' Traverse.
For Each company In xml.Element("LIST").Elements("COMPANY")
Console.WriteLine(String.Format("Company: {0}", company.Element("A_COMPANY").Value))
For Each gBreak In company.Element("LIST_BREAK").Elements("G_BREAK")
Console.WriteLine(String.Format("Currency: {0}", gBreak.Element("CURRENCY").Value))
Console.WriteLine(String.Format("Sort Column: {0}", gBreak.Element("SORT_COLUMN").Value))
Next
Next
'GetXML()函数只返回一个字符串,它可以来自文件,db,XML文字等。
代码经过测试并产生以下内容......
公司:这里的公司
货币:2
排序栏:100
货币:25
排序栏:130
货币:77
排序栏:1350
Private Function GetXML() As String
Return <POX>
<LIST>
<COMPANY>
<A_COMPANY>The company here</A_COMPANY>
<A_ID>786</A_ID>
<A_BASE>USD</A_BASE>
<A_YES>Yes</A_YES>
<A_NO>No</A_NO>
<A_CATEGORY_D>1210043021</A_CATEGORY_D>
<A_PRECISION>2</A_PRECISION>
<LIST_BREAK>
<G_BREAK>
<CURRENCY>2</CURRENCY>
<SORT_COLUMN>100</SORT_COLUMN>
</G_BREAK>
<G_BREAK>
<CURRENCY>25</CURRENCY>
<SORT_COLUMN>130</SORT_COLUMN>
</G_BREAK>
<G_BREAK>
<CURRENCY>77</CURRENCY>
<SORT_COLUMN>1350</SORT_COLUMN>
</G_BREAK>
</LIST_BREAK>
</COMPANY>
</LIST>
</POX>.ToString()
End Function
答案 1 :(得分:0)
忽略不使用LINQ to XML,建议跳出的错误是
Dim strTest1 = m_xmlr.GetAttribute("/POX/LIST/COMPANY/LIST_BREAK/G_BREAK")
GetAttribute是获取xml属性,因此节点GetAttribute(“attr”)将返回“1”。使用XmlTextReader无法xpath到节点,您必须自己编写xml遍历代码。