Search code examples
c#linq-to-xmlxelementxattributelinq-to-xsd

C# : Get Specific Element Before another Element with Linq


Consider the following snippet from an XSD:

    <xs:complexType name="AccountIdentification4ChoiceGPS">
    <xs:choice>
        <xs:element name="IBAN" type="IBAN2007Identifier">
            <xs:annotation>
                <xs:appinfo>
                    <xs:logicalname>IBAN</xs:logicalname>
                    <xs:additionalInfo>[A-Z]{2,2}[0-9]{2,2}[a-zA-Z0-9]{1,30}</xs:additionalInfo>
                    <xs:ISOType>(CashAccount20)</xs:ISOType>
                </xs:appinfo>
                <xs:documentation>Unambiguous identification of the account as International Bank Account Number (IBAN).</xs:documentation>
            </xs:annotation>
        </xs:element>
        <xs:element name="Othr" type="GenericAccountIdentification1GPS">
            <xs:annotation>
                <xs:appinfo>
                    <xs:logicalname>Other</xs:logicalname>
                    <xs:ISOType>(CashAccount20)</xs:ISOType>
                </xs:appinfo>
            </xs:annotation>
        </xs:element>
    </xs:choice>
</xs:complexType>

Using LINQ: If I know element name="IBAN" how can I retrieve element <xs:complexType name="AccountIdentification4ChoiceGPS"> if I don't know much about content of my XSD.`? The only think I may know is that the LocalName is "complexType";

I am thinking of :

    IEnumerable<XElement> elementAbove = xsdDocument.Descendants()
.Where(x=>x.Name.LocalName == "complexType")
.Select(Get here the value of the element that could be the parent or the parent's parent).

This is where I get stuck.


Solution

  • Try:

    IEnumerable<XElement> elementAbove = xsdDocument.Descendants()
       .Where(x => x.Name.LocalName == "complexType" && x.Descendants().Any(y => y.Name.LocalName == "element" && y.Attributes().Any(z => z.Value == "IBAN")));