C#解析XML Schema(SimpleType)问题

时间:2009-12-10 08:12:52

标签: c# xml xsd

美好的一天。

我可以处理ComplexType,例如:

  <xsd:element name="Prerequisite">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Type" type="Prerequisite.Type.type" minOccurs="1" maxOccurs="1" />
        <xsd:element name="Miscellaneous" type="Prerequisite.Misc.type" minOccurs="0" maxOccurs="1" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

我的C#如下所示:

    // Get the sequence particle of the complex type.
    XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;

    if (sequence != null)
    {
        // Iterate over each XmlSchemaElement in the Items collection.
        foreach (XmlSchemaElement childElement in sequence.Items)
        {
            Console.WriteLine("Element: {0}/{1}/{2}/{3}", 
                              childElement.QualifiedName.Name,
                              childElement.SchemaTypeName.Name, 
                              childElement.MinOccurs, 
                              childElement.MaxOccurs);
        }
    }

但我不知道如何处理“限制”和“枚举”? 你能给我一些指导吗?提前谢谢。

  <xsd:simpleType name="SystemTypeEnum">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="Etch" />
    </xsd:restriction>
  </xsd:simpleType>

XmlSchemaSequence - &gt;我找不到 XmlSchemaRestriction

的类

XmlSchemaElement - &gt;我找不到 XmlSchemaEnumeration 的类。

: - (

=========================需要进一步的帮助。谢谢。 ========================

@ foreach (XmlSchemaEnumerationFacet childEnumeration in ????);

我仍然不知道如何完成顶线。   你能给我进一步的帮助吗?

        XmlSchemaSimpleType simpleType = xsdType as XmlSchemaSimpleType;
        Console.WriteLine("simpleType: {0}", xsdType.Name);
        XmlSchemaSimpleTypeRestriction restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction;

        if (restriction != null)
        {

            Console.WriteLine("restriction : {0}", restriction.BaseTypeName.Name);

            foreach (XmlSchemaEnumerationFacet childEnumeration in ????)
            {
                Console.WriteLine("Element: {0}", childEnumeration.Value);
            }
        }

2 个答案:

答案 0 :(得分:2)

    XmlSchemaSimpleType simpleType = xsdType as XmlSchemaSimpleType;
    Console.WriteLine("simpleType: {0}", xsdType.Name);
    XmlSchemaSimpleTypeRestriction restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction;

    if (restriction != null)
    {

        Console.WriteLine("restriction : {0}", restriction.BaseTypeName.Name);

        foreach (XmlSchemaObject facet in restriction.Facets)
        {
            if (facet is XmlSchemaEnumerationFacet)

            Console.WriteLine("Element: {0}", ((XmlSchemaEnumerationFacet)facet).Value);
        }
    }

答案 1 :(得分:1)