Site Search:
Sign in | Join | Help
4Penny.net

Steve Gray

Random Thoughts from the 4Penny VP of Development

Iterating through an XML file

Given the XML structure:

 <employees>
 <employee>
  <firstname>John</firstname>
  <lastname>Doe</lastname>
 </employee>
 <employee>
  <firstname>James</firstname>
  <lastname>Buck</lastname>
 </employee>
 <employees>


 

I sometimes need to loop (iterate) through the file and gather the first/last names

Here is a piece of code that will do that

            Dim xdoc As New XmlDocument
            xdoc.Load(URL)


            'get a nodeList filled with employee nodes
            Dim hNodes As XmlNodeList = xdoc.SelectNodes("employees/employee")
            For Each hNode As XmlNode In hNodes
                'get first and last names, store them
                strFirstName= hNode.SelectSingleNode("firstname").InnerText
                strLastName= hNode.SelectSingleNode("lastname").InnerText


            Next

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.