Looping through nodes with XPathNavigator

qnfund

New Member
Hi below is a sample of the xml i am using. I been through allsorts of options I can think of to be able to start at the personData node and iterate the results, and nothing i seem to try works unless I navigate manually through each child node from the root down. Can anyone advise on how I can do this without starting at the rootMy code currently is \[code\]using (var r = File.OpenText(@"C:\S\sp.xml")) { XPathDocument document = new XPathDocument(XmlReader.Create(r)); XPathNavigator xPathNav = document.CreateNavigator(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xPathNav.NameTable); nsmgr.AddNamespace("g2", "http://person.transferobject.com/xsd"); XPathNodeIterator xni = xPathNav.Select("/g2:companys/g2:company/g2:person/g2:personData", nsmgr); foreach (XPathNavigator nav in xni) Console.WriteLine(nav.Name); }\[/code\]Xml\[code\]<?xml version="1.0" encoding="UTF-8"?><Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><dataSource xmlns="http://person.transferobject.com/xsd">IG2</dataSource><dateTime xmlns="http://person.transferobject.com/xsd">Thu Mar 21 15:56:42 GMT 2013</dateTime><formatVersion xmlns="http://person.transferobject.com/xsd">2.0</formatVersion><companys xmlns="http://person.transferobject.com/xsd"><company> <errorMessages xsi:nil="true"/> <person> <personData> <address> <address1 xmlns="http://transferobject.com/xsd">37 Smith St</address1> <county xmlns="http://transferobject.com/xsd">COUNTY-37</county> <postcode xmlns="http://transferobject.com/xsd">Po12 123</postcode> </address> <basicDetails> <currentFirstName xmlns="http://transferobject.com/xsd">Fred</currentFirstName> <currentLastName xmlns="http://transferobject.com/xsd">Bloggs</currentLastName > <currentStage xmlns="http://transferobject.com/xsd">H1</currentStage> <currentGroup xmlns="http://transferobject.com/xsd">3</currentGroup> <dob xmlns="http://transferobject.com/xsd">2000-04-25</dob> <email xmlns="http://transferobject.com/xsd">[email protected]</email> <entryDate xmlns="http://transferobject.com/xsd">2003-09-03</entryDate> </basicDetails> </personData> <personData> <address> <address1 xmlns="http://transferobject.com/xsd">37 Smith St</address1> <county xmlns="http://transferobject.com/xsd">COUNTY-37</county> <postcode xmlns="http://transferobject.com/xsd">Po12 123</postcode> </address> <basicDetails> <currentFirstName xmlns="http://transferobject.com/xsd">John</currentFirstName> <currentLastName xmlns="http://transferobject.com/xsd">Bloggs</currentLastName > <currentStage xmlns="http://transferobject.com/xsd">H1</currentStage> <currentGroup xmlns="http://transferobject.com/xsd">3</currentGroup> <dob xmlns="http://transferobject.com/xsd">1999-04-25</dob> <email xmlns="http://transferobject.com/xsd">[email protected]</email> <entryDate xmlns="http://transferobject.com/xsd">2003-09-03</entryDate> </basicDetails> </personData> </person></company></companys></header>\[/code\]
 
Back
Top