Monday, November 12, 2012 12:57 PM
I would like to read through a XML document but I am unfamiliar with the XMLDocument and the XDocument classes. I would appreciate a little advice on how to do this using Visual Basic 2010.
Here is what I would like to do. in my main procedure I want to open the file and read all the "top" level nodes. Believe it or not, this i where I am stuck. I can open the file. But when I try to read Elements, I get all the elements and not just the top elements.
Here is my code.
Dim document As XDocument = XDocument.Load("M:\mpdillon\Red Cheetah\export_specs.xml") For Each curElement As XElement In document...<redcheetah> For Each curDesc As XElement In curElement.Descendants MsgBox(curDesc.Name.ToString) Next Next
What would be the correct code to get just the direct children of the node redcheetah?
These nodes are:address, customer, user, cost-center, customer-purchase-order, credit memo, order, quote and purchase-order.
The second part of my question has to do with reading all the child elements of a parent and detecting when all those elements have been read. For example: Once I determined by the above code that I was on the "cost-center" node, I want to read all of its elements and stop once all the elements have been read.
I have been trying to use the above code unsuccessfully to do that.
The example XML file I am trying to read can be found in the link below. It is 17 KB.
Thanks for you assistance.
- Edited by mpdillon56 Monday, November 12, 2012 12:58 PM
Monday, November 12, 2012 1:33 PM
Consider to spend some time with the documentation, either online at http://msdn.microsoft.com/en-us/library/bb387098.aspx or in your local Visual Studio documentation.
As for your first question, if you want to access the child elements use Elements() instead of Descendants().
You also shouldn't use document...<redcheetah> to simply access the root element, doing
suffices, or if you want to go by the name then do
If you want to access the "cost-center" element use
to access its child elements you would access
As for stopping once all elements have been read, with LINQ to XML your XDocument.Load() call loads the complete document into memory, then you can access any nodes as needed, there is really no need to explicitly do something like "stopping reading".
MVP Data Platform Development My blog
- Marked As Answer by mpdillon56 Monday, November 12, 2012 2:08 PM
Monday, November 12, 2012 2:08 PM
Thank you that was very helpful.
I continued to work on this while you were reading. Below is my code to do what I need. However, I will modify my code to incorporate your notation <redcheetah>.<cost-center>.
Dim FileNameString As String = "M:\mpdillon\Red Cheetah\export_specs.xml" Dim document As New System.Xml.XmlDocument() Dim TextString As String = String.Empty Dim i As Integer = 0 document.Load(FileNameString) 'Use this to count the number of top level nodes, such as address, customer, order, etc. For Each node As Xml.XmlNode In document.SelectSingleNode("redcheetah") TextString = node.Name ' 'May be useful for exploring the child node as part of Order validation 'For Each curChild As Xml.XmlNode In node.ChildNodes 'TextString = curChild.Name.ToString 'Next Next ' 'Returns all the nodes with this exaxt name. ' For Each node As Xml.XmlNode In document.SelectNodes("redcheetah/address") TextString = node.InnerText i = 0 'Look for/validate that the necessary attributes are present. For Each curAttr As Xml.XmlAttribute In node.Attributes Select Case node.Attributes(i).Name.ToString Case "id" TextString = node.Attributes(i).Value End Select i += 1 Next Next '
- Edited by mpdillon56 Monday, November 12, 2012 2:10 PM