【问题标题】:C# : Get Specific Element Before another Element with LinqC#:使用 Linq 在另一个元素之前获取特定元素
【发布时间】:2019-06-27 19:57:24
【问题描述】:

考虑以下来自 XSD 的 sn-p:

    <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" 我如何检索元素 &lt;xs:complexType name="AccountIdentification4ChoiceGPS"&gt; 如果我不太了解我的 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).

这就是我卡住的地方。

【问题讨论】:

标签: c# linq-to-xml xelement xattribute linq-to-xsd


【解决方案1】:

试试:

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

【讨论】:

  • 几乎做到了。 z.Name 应该是 z.Value。谢谢马特!
猜你喜欢
  • 2015-06-02
  • 2021-11-04
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 2015-12-03
  • 1970-01-01
相关资源
最近更新 更多