解析XSD限制

时间:2015-05-26 10:33:14

标签: c# xml visual-studio xsd schema

    <xs:group name="datosGeneralesComunes">
    <xs:sequence>
        <xs:element name="gravedad">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1" />
                    <xs:maxInclusive value="4" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="zona">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1" />
                    <xs:maxInclusive value="4" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element minOccurs="0" name="descripcion">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="2000" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element minOccurs="0" name="tipo_accidente_salida">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1" />
                    <xs:maxInclusive value="2" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element minOccurs="0" name="tipo_accidente_colision">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1" />
                    <xs:maxInclusive value="13" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element minOccurs="0" name="salida_via">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:enumeration value="0" />
                    <xs:enumeration value="1" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element minOccurs="0" name="tipo_accidente_animal">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="0" />
                    <xs:maxInclusive value="27" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="unidad_demarcacion">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:maxLength value="10" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:group>

我需要解析组内的XSD文件的限制,但我只能看到(在调试视图中)元素的名称(我也看到“派生自限制”)。但我需要minInclusive和maxInclusive值。

到目前为止,这是我的代码:

XmlSchema schema;

                        using (var reader = new StreamReader(Path.Combine(Environment.CurrentDirectory, "esquema.xsd")))
                        {
                            schema = XmlSchema.Read(reader, null);
                        }

                        XmlSchemaSet schemaSet = new XmlSchemaSet();
                        schemaSet.Add(schema);
                        schemaSet.Compile();

编译架构并进入调试模式后,我看不到我需要什么。有线索吗?

此致

1 个答案:

答案 0 :(得分:1)

好的,我们假设我们想获得架构的第21项(就像我的XSD片段一样)。以下是所有步骤(我还包括动态打印,而不是检查所有可能的限制类型)。希望将来对某人有所帮助......

schema = schemaSet.Schemas().Cast<XmlSchema>().Single();
XmlSchemaGroup g = schema.Items[21] as XmlSchemaGroup;
XmlSchemaElement ge = g.Particle.Items[0] as XmlSchemaElement;
XmlSchemaSimpleType gef = ge.ElementSchemaType as XmlSchemaSimpleType;
XmlSchemaSimpleTypeContent gefc = gef.Content as XmlSchemaSimpleTypeContent;
XmlSchemaSimpleTypeRestriction re = gefc as XmlSchemaSimpleTypeRestriction;
XmlSchemaObjectCollection fac = re.Facets;
foreach (var restriccion in fac)
{
    Console.WriteLine(restriccion.GetType().ToString()+": {0}", (restriccion as dynamic).Value);
    //if (restriccion.GetType().Equals(typeof(XmlSchemaLengthFacet)))
    //{
    //    Console.WriteLine("length: {0}", (restriccion as XmlSchemaLengthFacet).Value);
    //}
    //else if (restriccion.GetType().Equals(typeof(XmlSchemaMinInclusiveFacet)))
    //{
    //    Console.WriteLine("minInclusive: {0}", (restriccion as XmlSchemaMinInclusiveFacet).Value);
    //}
    //else if (restriccion.GetType().Equals(typeof(XmlSchemaMaxInclusiveFacet)))
    //{
    //    Console.WriteLine("maxInclusive: {0}", (restriccion as XmlSchemaMaxInclusiveFacet).Value);
    //}
}