C#:使用 Linq 在另一个元素之前获取特定元素
C# : Get Specific Element Before another Element with Linq
考虑 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>
使用 LINQ:如果我知道 element name="IBAN"
我该如何检索元素 <xs:complexType name="AccountIdentification4ChoiceGPS">
如果我对 XSD.` 的内容不太了解?我可能唯一知道的是 LocalName 是 "complexType";
我在想:
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).
这是我卡住的地方。
尝试:
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")));
考虑 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>
使用 LINQ:如果我知道 element name="IBAN"
我该如何检索元素 <xs:complexType name="AccountIdentification4ChoiceGPS">
如果我对 XSD.` 的内容不太了解?我可能唯一知道的是 LocalName 是 "complexType";
我在想:
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).
这是我卡住的地方。
尝试:
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")));