LINQ C#:获取在所有子XML元素中通用的不同TAG名称(非值)

时间:2013-04-24 19:44:01

标签: c# linq linq-to-xml

<root>
    <abc:Description abc:about="XXX.XXX_CSData-2">
        <xxx:Data.Curve abc:resource="XXX.XXX"/>
        <xxx:Data.y2AData abc:datatype="#int">27</xxx:Data.y2AData>
        <xxx:Data.y1AData abc:datatype="#int">-27</xxx:Data.y1AData>
        <xxx:Data.xAData abc:datatype="#int">60</xxx:Data.xAData>
        <xxx:IdentifiedObject.description abc:datatype="#string">SOME NAME</xxx:IdentifiedObject.description>
        <xxx:IdentifiedObject.name abc:datatype="#string">XXX_CCC.XX</xxx:IdentifiedObject.name>
        <abc:type abc:resource="http://iec.ch/TC57/2008/xxx-schema-xxx13#Data"/>
      </abc:Description>
      <abc:Description abc:about="XXX.XXX">
        <xxx:ConnectivityNode.MemberOf_EquipmentContainer abc:resource="XXX.XXX"/>
        <xxx:IdentifiedObject.description abc:datatype="#string">XXX.XXX</xxx:IdentifiedObject.description>
        <xxx:IdentifiedObject.name abc:datatype="#string">XXX.XXX</xxx:IdentifiedObject.name>
        <abc:type abc:resource="http://iec.ch/TC57/2008/xxx-schema-xxx13#ConnectivityNode"/>
        <xxx:ConnectivityNode.Terminals abc:resource="XXX.XXX"/>
        <xxx:ConnectivityNode.Terminals abc:resource="XXX.XXX"/>
        <xxx:ConnectivityNode.Terminals abc:resource="JXXX.XXX"/>
        <xxx:ConnectivityNode.Terminals abc:resource="JXXX.XXX"/>
      </abc:Description>
</root>

大家好,

在上面的XML Snippet中,标签是“xxx:IdentifiedObject.description”,“IdentifiedObject.name”和“abc:type”在两个子节点之间是通用的。

我想编写一个LINQ查询,该查询将返回子元素中常见的(至少出现一次)这些标记名称。这需要tage名称,但需要标签的值 1)“xxx:IdentifiedObject.description”, 2)“IdentifiedObject.name”和 3)“abc:type”

1 个答案:

答案 0 :(得分:2)

听起来你想要这样的东西:

// Given x and y as the parent elements you're interested in
var commonNames = x.Elements()
                   .Select(x => x.Name)
                   .Intersect(y.Elements().Select(y => y.Name));

这会给你一个IEnumerable<XName>