2

我有一个 Xml 文件,我想在其中制作基于 XPath 的过滤器。我尝试了很多例子,它们都没有帮助毫无疑问我做错了。我是 XPath 的新手,请帮我找到解决方案。

<PeopleBatch Counter="3">
<Citriz ID="1d9a88fe-f9cc-4add-b6d1-01e41c561bfb" mVersion="1.0.0" mSequence="1" pVersion="0.0.1" xmlns="http://Citriz/Schemas">
    <People Action="U" Status="CD" PeopleID="1" PeopleShortName="Billy" PeopleLongName="Billy Smith" PeopleTypeCode="Commercial" CountryCode="USA" PeopleStatus="Current">
            <PeopleRole Action="U" Status="CD" ID="1" CustomerRoleShortName="Billy" CustomerRoleLongName="Billy Smith" TypeCode="OUTS">
                    <SendRole RoleType="N" ActiveRole="Y"/>
            </PeopleRole>
    </People>
</Citriz>
<Citriz ID="1d9a88fe-f9cc-4add-b6d1-01e41c561bfc" mVersion="1.0.0" mSequence="2" pVersion="0.0.1" xmlns="http://Citriz/Schemas">
    <People Action="U" Status="CD" PeopleID="2" PeopleShortName="Carl" PeopleLongName="Carl Thomas" PeopleTypeCode="Commercial" CountryCode="USA" PeopleStatus="Current">
            <PeopleRole Action="U" Status="CD" ID="2" CustomerRoleShortName="Carl" CustomerRoleLongName="Carl Thomas" TypeCode="INSS">
                    <SendRole RoleType="N" ActiveRole="Y"/>
            </PeopleRole>
    </People>
</Citriz>   
<Citriz ID="1d9a88fe-f9cc-4add-b6d1-01e41c561bfd" mVersion="1.0.0" mSequence="3" pVersion="0.0.1" xmlns="http://Citriz/Schemas">
    <People Action="U" Status="CD" PeopleID="3" PeopleShortName="Ryan" PeopleLongName="Ryan Black" PeopleTypeCode="Commercial" CountryCode="USA" PeopleStatus="Current">
            <PeopleRole Action="U" Status="CD" ID="3" CustomerRoleShortName="Ryan" CustomerRoleLongName="Ryan Black" TypeCode="INSS">
                    <SendRole RoleType="N" ActiveRole="Y"/>
            </PeopleRole>
    </People>
</Citriz>   

我需要所有那些子节点属性包含 TypeCode="INSS" 这个值的“Citriz”节点。或者如果有其他好的方法,请建议我。

4

3 回答 3

6

鉴于您使用的是 LINQ to XML,我不会从 XPath 开始。我会使用:

XNamespace ns = "http://Citriz/Schemas";
var nodes = doc.Descendants(ns + "Citriz")
               .Where(x => x.Descendants()
                            .Any(y => (string) x.Attribute("TypeCode") == "INSS"));

或者,如果它总是内部PeopleRole的元素(每个级别只有一个元素):PeopleCitriz

XNamespace ns = "http://Citriz/Schemas";
var nodes = doc.Descendants(ns + "Citriz")
               .Where(x => (string) x.Element(ns + "People")
                                     .Element(ns + "PeopleRole")
                                     .Attribute("TypeCode") == "INSS"));

我确信这可以在 XPath 中明智地完成,但我个人认为 LINQ to XML 更简单,将数据部分(元素名称、预期值等)与“代码”部分(“我正在寻找为后代”或“我正在寻找一个属性值”)。

于 2013-05-02T19:57:17.430 回答
1

我认为您对 Xml 命名空间有疑问

XNamespace ns = "http://Citriz/Schemas";
var peopleRole = XDocument.Parse(xml)
                    .Descendants(ns + "PeopleRole")
                    .Where(p => p.Attribute("TypeCode").Value == "INSS")
                    .ToList();
于 2013-05-02T20:22:18.430 回答
0

您可以认为 XPath 相当于在文件系统中导航目录树。向 xpath 查询添加额外的“目录”可以让您更深入地了解 DOM 树。

//PeopleRole[@TypeCode="inss"]/../..

将查找 PeopleRole 类型的所有节点,其 TypeCode 属性等于“inss”,然后向上移动到树中的级别,返回<Citriz>包含匹配的 PeopleRoles 的节点

于 2013-05-02T19:58:14.787 回答