code_junkie

How do I use XPathNodeIterator to iterate through a list of items in an XML file?

Nov 11th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. <HostCollection>
  2. <ApplicationInfo />
  3. <Hosts>
  4. <Host>
  5. <Name>Test</Name>
  6. <IP>192.168.1.1</IP>
  7. </Host>
  8. <Host>
  9. <Name>Test</Name>
  10. <IP>192.168.1.2</IP>
  11. </Host>
  12. </Hosts>
  13. </HostCollection>
  14.  
  15. Dim doc As XmlDocument = New XmlDocument()
  16. doc.Load("hosts.xml")
  17. Dim nodeList as XmlNodeList
  18. nodeList = doc.SelectNodes("/HostCollectionInfo/Hosts/Host")
  19.  
  20. XPathDocument xpathDoc;
  21. using (StreamReader input = ...)
  22. {
  23. xpathDoc = new XPathDocument(input);
  24. }
  25.  
  26. XPathNavigator nav = xpathDoc.CreateNavigator();
  27. XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
  28.  
  29. XPathNodeIterator nodes = nav.Select("/HostCollection/Hosts/Host", nsmgr);
  30.  
  31. while (nodes.MoveNext())
  32. {
  33. // access the current Host with nodes.Current
  34. }
Add Comment
Please, Sign In to add comment