Link to home
Start Free TrialLog in
Avatar of Stacey Fontenot
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.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

you can use the ReadXML method of DataSet in order to load the XML content into a DataSet, from there you can do the manipulation you wanted.

or use XMLDocument class to load the XML content.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Fernando Soto

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.
if you have a string, and know the structure, you can use them like

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

Open in new window


if you have a file, comment out line 2-3, un-comment line 4