【发布时间】:2021-12-10 23:16:34
【问题描述】:
我已经搜索了 S/O,但没有找到任何可以解决我的问题的方法(尽管 C# - Parsing XSD schema - get all elements to combobox 帮助了一点)。
在下面的 XSD 文件中,我需要获取嵌套元素的名称(SectionA 然后到 AX010_1、AX040_1.. 然后到 SectionB 和 BX010_1、BX070_1 等)
如何访问嵌套元素?
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Vehicle">
<xs:complexType>
<xs:sequence>
<xs:element name="Ford" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="SectionA" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="AX010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX040_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX050_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX190_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="A080_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX230F_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX230G_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX230GNOTE_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX230H_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX230HNOTE_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
<xs:element name="AX230J_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SectionB" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="BX010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="BX070_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="BX350N_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="BX350NNOTE_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SectionC" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="C010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="C010_2" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="C010_4" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="CF030_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SectionD" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="D010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="D030_2" type="Dollar" minOccurs="0" maxOccurs="1"/>
<xs:element name="D560_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
【问题讨论】:
-
到目前为止你的代码是什么?递归搜索怎么样?对于文档中的每个 XElement,递归搜索所有子元素,直到找到属性名称为 SectionA 的子元素?