Stacey Fontenot
asked on
Reading an XML Document
I need some code that will allow me to read an xml document that several levels of nodes with attributes and ultimately place these values in a data table.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Fernando Soto
that code looks like just for one level...
to traverse all nodes we need a recursive function somehow...
that code looks like just for one level...
to traverse all nodes we need a recursive function somehow...
@Hain Kurt;
Actually the Descendants method of the XDocument without any parameter will return all the nodes from the XML document so that should be all that is needed.
Actually the Descendants method of the XDocument without any parameter will return all the nodes from the XML document so that should be all that is needed.
if you have a string, and know the structure, you can use them like
if you have a file, comment out line 2-3, un-comment line 4
Public Sub EE29052976()
Dim xml as string = "<users><user><name>Hain</name><contact><phone>555666777</phone><email>hk@domain.com</email></contact><login>HK</login></user><user><name>Kurt</name><login>KH</login></user></users>"
Dim xdoc = System.Xml.Linq.XDocument.Parse(xml)
'Dim xdoc = XDocument.Load("C:\Working Directory\Users.xml")
for each user as XElement in xdoc.Elements("users").Elements("user")
Console.WriteLine("name: " & user.Element("name").Value)
Console.WriteLine("login: " & user.Element("login").Value)
next
end sub
if you have a file, comment out line 2-3, un-comment line 4
or use XMLDocument class to load the XML content.