遍历.net中xsd文件中“根元素”的所有元素,该元素是一个complextype
本文关键字:元素 complextype 一个 文件 xsd net 遍历 根元素 | 更新日期: 2023-09-27 18:06:21
我需要帮助来遍历xsd文件中的所有元素。我对这门学科还是新手。我粘贴了下面的示例代码,我一直工作到现在。
首先,我只是将xsd文件(如代码片段所示)加载到xml模式中,然后需要遍历根元素中的元素。
XmlSchema xsd = XmlSchema.Read(new StreamReader(AppKeysManager.ConfigurationMasterFolder + @"'Parameters.xsd"), null);
var xss = new XmlSchemaSet();
xss.Add(xsd);
xss.Compile();
foreach (DictionaryEntry item in xsd.Elements)
{
rootElement = item.Value as XmlSchemaElement; break;
}
xsd的代码片段
<xs:complexType name="Parameters">
<xs:all>
<xs:element name="A">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="mstns:Restricted8CharString">
<xs:attribute name="Caption" use="required" fixed="Caption for A">
<xs:simpleType>
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="ActionWhenMaxReached" use="required">
<xs:simpleType>
<xs:restriction base="xs:short">
<xs:pattern value="[1-3]"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="Expression" type="xs:string" default="0" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="B">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:short">
<xs:attribute name="Caption" use="prohibited">
<xs:simpleType>
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="ActionWhenMaxReached" use="required">
<xs:simpleType>
<xs:restriction base="xs:short">
<xs:pattern value="[1-3]"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="Expression" type="xs:string" default="0" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType name="Parameters">
下面的代码将为您提供内部元素及其属性
XmlSchema xsd = XmlSchema.Read(new StreamReader(pathToxsdFile), null);
var xss = new XmlSchemaSet();
xss.Add(xsd);
xss.Compile();
XmlSchemaElement rootElement = null;
foreach (DictionaryEntry item in xsd.Elements)
{
rootElement = item.Value as XmlSchemaElement; break;
}
XmlSchemaComplexType innerContent = rootElement.ElementSchemaType as XmlSchemaComplexType;
var innerContentsOfRoot = innerContent.Particle as XmlSchemaAll;
foreach (XmlSchemaElement item in innerContentsOfRoot.Items)
{
XmlSchemaComplexType moreInnerContent = item.ElementSchemaType as XmlSchemaComplexType;
foreach (DictionaryEntry item2 in moreInnerContent.AttributeUses)
{
if (string.Compare(((XmlSchemaAttribute)(item2.Value)).Name, attributeName, true) == 0)
{
string captionName = ((XmlSchemaAttribute)(item2.Value)).FixedValue;
}
}
}