使用具有嵌套索引的XPath从root用户访问特定的Xmlnode

时间:2014-08-18 22:30:14

标签: c# xml xpath

我有这个xml字符串,我必须得到一个特定的节点

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.XPath;

//具有xml内容的Xml字符串

string xmlStr = "<Candidate>
    <Content>
    <DOB>14-Jan-1976</DOB>
    <Employers>
      <Employer>
        <Name>Diane Trucking</Name>
        <Addresses>
          <Address>
            <AddressLine1>1st Street</AddressLine1>
            <AddressLine2/>
            <City>First City</City>
            <State>FL</State>
            <Zip>12345</Zip>
          </Address>
          <Address>
            <AddressLine1>1st Street</AddressLine1>
            <AddressLine2/>
            <City>Second City</City>
            <State>FL</State>
            <Zip>12346</Zip>
          </Address>
          <Address>
            <AddressLine1>3rd Street</AddressLine1>
            <AddressLine2/>
            <City>Third City</City>
            <State>FL</State>
            <Zip>12347</Zip>
          </Address>
        </Addresses>
      </Employer>
      <Employer>
        <Name>Tom Trucking</Name>
        <Addresses>
          <Address>
            <AddressLine1>4th Street</AddressLine1>
            <AddressLine2/>
            <City>Fourth City</City>
            <State>FL</State>
            <Zip>12348</Zip>
          </Address>
          <Address>
            <AddressLine1>5th Street</AddressLine1>
            <AddressLine2/>
            <City>Fifth City</City>
            <State>FL</State>
            <Zip>12349</Zip>
          </Address>
          <Address>
            <AddressLine1>6th Street</AddressLine1>
            <AddressLine2/>
            <City>Sixth City</City>
            <State>FL</State>
            <Zip>12340</Zip>
          </Address>
        </Addresses>
      </Employer>
    </Employers>
    </Content>
    </Candidate>";

//创建xml文档

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlStr);

导航器用于替代测试

        XPathNavigator xPathNav = xmlDoc.CreateNavigator();
        XPathExpression expr = XPathExpression.Compile("/Candidate/Content/Employers['1']/Employer['1']/Addresses['1']/Address");

表达式读取/定位特定于xml的xml节点

        XPathNodeIterator xPathIt = xPathNav.Select(expr);


if(xPathIt == null)
{

... xPathIt.Count也是6

}

var xmlNodes1 = xmlDoc.SelectNodes("/Candidate/Content/Employers['1']/Employer['1']/Addresses['1']/Address");

for (int i = 0; i < xmlNodes1.Count; i++ )  
{

... xmlNodes1.Count = 6.返回所有6个地址节点,期望特定雇主/雇主/地址/地址的3个“地址”节点

}

xPathIt和xmlNodes1都返回6个“Address”元素 我预计它会返回3个元素,因为雇主有3个地址

我想从root中定位xml中的任何元素 此外,如果我能做到这一点,我将能够更新特定元素。

感谢帮助 谢谢

1 个答案:

答案 0 :(得分:0)

尝试从XPath中删除单引号:

var xmlNodes1 = xmlDoc.SelectNodes("/Candidate/Content/Employers['1']/Employer['1']/Addresses['1']/Address");

应该是

var xmlNodes1 = xmlDoc.SelectNodes("/Candidate/Content/Employers[1]/Employer[1]/Addresses[1]/Address");

请注意,数字1周围的单引号将被删除。

选择第一项的谓词是[1]['1']会忽略使用谓词SelectNodes,因此您将获得所有节点,而不仅仅是第一个节点。