使用XPathNavigator循环遍历节点

时间:2013-03-22 13:08:06

标签: c# xml xpath

下面是我正在使用的xml示例。我已经通过各种各样的选项,我能想到能够从personData节点开始并迭代结果,除非我从根目录手动浏览每个子节点,否则我似乎没有尝试。任何人都可以建议我如何在没有从根

开始的情况下做到这一点

我的代码目前是

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);
        }

XML

<?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">AN@AN.AOM</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">AN@AN.AOM</email>
                <entryDate xmlns="http://transferobject.com/xsd">2003-09-03</entryDate>
            </basicDetails>
        </personData>
    </person>
</company>
</companys>
</header>

2 个答案:

答案 0 :(得分:3)

我知道你正在使用XPath,但是当你得到XPath的答案时我会用Linq给出一个

using System;
using System.Linq;
using System.Xml.Linq;

namespace xmlTest
{
    class Program
    {
        static void Main()
        {
            XDocument doc = XDocument.Load("C:\\Users\\me\\Desktop\\so.xml");
            var personDataDetails = (from p in doc.Descendants().Elements()                                     
                                     where p.Name.LocalName == "personData"
                                         select p);

            foreach (var item in personDataDetails)
            {
                Console.WriteLine(item.ToString());
            }

            Console.ReadKey();
        }
    }
}

答案 1 :(得分:0)

您是否只是询问如何在不列出完整路径的情况下迭代personData个节点?如果这就是你想要做的,你可以这样做:

XPathNodeIterator xni = xPathNav.Select("//g2:personData", nsmgr);