Reputation: 10552
I am trying to parse this XML file with this type of layout:
<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........
And i cant seem to get this code working for it:
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
I get an error on
Dim **strTest2 = m_xmlr.ReadElementString("CURRENCY")**
Saying:
Element 'CURRENCY' was not found. Line 4, position 4.
Also, strTest1 returns as nothing
What could i be doing incorrectly?
Thanks!
update This is working but it doesn't seem to get all the items..
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
The vendorName gets a total of x = 1435 The vendorAddress and vendorTotal gets a total of x = 1481..... Thats not going to be good when putting the values all together....
David
Upvotes: 1
Views: 848
Reputation: 4780
Ignoring not using LINQ to XML as suggested an error that leaps out is
Dim strTest1 = m_xmlr.GetAttribute("/POX/LIST/COMPANY/LIST_BREAK/G_BREAK")
GetAttribute is to get an xml attribute so with the node GetAttribute("attr") would return "1". With an XmlTextReader there are no way to xpath to a node, you have to code the xml traversal yourself.
Upvotes: 0
Reputation: 1944
You can use this for starters..
' 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
' The GetXML() function just returns a string, it can come from a file, db, XML literal etc.
Code was tested and yielded the following...
Company: The company here
Currency: 2
Sort Column: 100
Currency: 25
Sort Column: 130
Currency: 77
Sort Column: 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
Upvotes: 1