简体   繁体   中英

LINQ C#: Get the Distinct TAG NAMES (Not Values) that's common across all child XML elements

<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>

Hello all,

In the above XML Snippet tha tags are "xxx:IdentifiedObject.description","IdentifiedObject.name" and "abc:type" are common between two child nodes.

I want to write a LINQ query that would return these tag names that are common (appear atleast once) in the child elements. That is need the tage names but the value of the tags 1) "xxx:IdentifiedObject.description", 2) "IdentifiedObject.name" and 3) "abc:type"

It sounds like you want something like:

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

That will give you an IEnumerable<XName> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM