Solved

Loop through XML file and read nodes

Posted on 2008-10-16
4
954 Views
Last Modified: 2012-05-05
need to loop through and XML file, and grab node vales ...

<users>
<user>
<name>Joe</name>
<oldfolderlocation> C:\oldfolder\ Joe </oldfolderlocation>
<newfolderlocation> C:\newfolder\ Joe </ newfolderlocation >
</ user >
< user >
<name>Tom</name>
<oldfolderlocation> C:\oldfolder\ Tom </oldfolderlocation>
<newfolderlocation> C:\newfolder\ Tom </ newfolderlocation >
</user>

</users>

Dim userNodes As XmlNodeList = doc.SelectNodes("people/ person")
 For Each userNodesNode As XmlNode In userNodes
My.Computer.FileSystem.CopyDirectory(userNodesNode("newfolderlocation ").InnerText, userNodesNode("oldfolderlocation").InnerText, False)

     Next
0
Comment
Question by:gvilla23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:rlh68
ID: 22737303

Not sure if it's ee or you xml but, make sure your xml doesn't have spaces in the elements tags, some of them did when I copied your xml to my code. The xmlDocument object may give you errors if your xml is not well formed.

i.e. this
 < user >
should be this
<user>
        Dim xDoc As New Xml.XmlDocument
        Dim xml As String
 
        xml = "<users><user><name>Joe</name><oldfolderlocation>C:\oldfolder\Joe</oldfolderlocation>" & _
           "<newfolderlocation>C:\newfolder\Joe</newfolderlocation></user><user><name>Tom</name>" & _
             "<oldfolderlocation>C:\oldfolder\Tom</oldfolderlocation><newfolderlocation>C:\newfolder\Tom</newfolderlocation></user></users>"
 
        xDoc.LoadXml(xml)
 
        Dim nl As Xml.XmlNodeList
        ' fill node list with users node
 
        nl = xDoc.GetElementsByTagName("users")
        ' loop each users
        For Each nUser As Xml.XmlNode In nl
            '' check for snippetIndex value we want to match
            'If n.SelectSingleNode("snippetIndex").InnerText = "0" Then
            '    ' show snippetName
            '    MessageBox.Show(n.SelectSingleNode("snippetName").InnerText)
            'End If
 
            Dim nName As Xml.XmlNode = nUser.SelectSingleNode("user/name")
            Dim nNewFolderLoc As Xml.XmlNode = nUser.SelectSingleNode("user/newfolderlocation")
            Dim nOldFolderLoc As Xml.XmlNode = nUser.SelectSingleNode("user/oldfolderlocation")
 
            MessageBox.Show(nName.InnerText & " | " & nNewFolderLoc.InnerText & " | " & nOldFolderLoc.InnerText)
 
        Next

Open in new window

0
 
LVL 8

Expert Comment

by:rlh68
ID: 22737309

oops, disregard the commented out code, it was from the original code that I used to write your example. I meant to delete it but forgot to.

Garbage code:
            '' check for snippetIndex value we want to match
            'If n.SelectSingleNode("snippetIndex").InnerText = "0" Then
            '    ' show snippetName
            '    MessageBox.Show(n.SelectSingleNode("snippetName").InnerText)
            'End If
0
 
LVL 19

Accepted Solution

by:
drichards earned 50 total points
ID: 22737983
If you are using the XML document for other purposes or if the document is small, the XmlDocument solution is fine.  If the document is large or performance is a concern then you will be better served by an XmlReader.

There are a number of approaches you could take with a reader, and I have included one here.  It's reasonably robust against XML comments and element whitespace.  It advances to each user node and then reads the name, oldfolderlocation, and newfolderlocation text elements.
        Dim rdr As XmlReader = XmlReader.Create(<xml file or stream>)
        While rdr.ReadToFollowing("user")
            rdr.ReadToFollowing("name")
            rdr.Read()
            Dim name As String = rdr.Value
            rdr.ReadToFollowing("oldfolderlocation")
            rdr.Read()
            Dim oldFolder As String = rdr.Value().Trim()
            rdr.ReadToFollowing("newfolderlocation")
            rdr.Read()
            Dim newFolder As String = rdr.Value().Trim()
            ' Do Copy here...
        End While

Open in new window

0
 

Author Closing Comment

by:gvilla23
ID: 31506981
Thank you very much for the explanation.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access Schema 6 54
VB.net Is there code behind PowerPivot 2 70
Convert VB web project to C# 3 65
Building Collections of Objects based on their Tag Property 33 54
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question