1

我需要帮助来遍历 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">
4

1 回答 1

0

下面的代码将为您提供内部元素及其属性

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;

            }
        }
    }
于 2013-07-16T07:40:12.223 回答