在 C# 中创建 xml 架构并进行验证

我是使用 XML 和 XML 模式的新手。


我有以下 XML 文件:


<?xml version="1.0" encoding="utf-8"?>

<MainDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.unece.org/cefact/namespaces/DocHeader">

  <DocHeader>

    <TypeVersion>5.0</TypeVersion>

    <Dest>

      <Id Auther="zzz">2</Id>

    </Dest>

  </DocHeader>

 </MainDoc>

我已经提到了https://www.w3schools.com/xml/schema_complex_text.asp


但它似乎不适用于我的情况(我有多个带有属性的元素),所以任何人都可以告诉我上述 XML 的确切架构是什么?


XML 架构:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="MainDoc">

<xs:complexType>

    <xs:sequence>

    <xs:element name="DocHeader">

    <xs:complexType>

        <xs:sequence>

            <xs:element name="TypeVersion" type="xs:double"/>               

            <xs:element name="Dest" type="xs:string">

                <xs:complexType>

                    <xs:sequence>

                        <xs:element name="Id">

                            <xs:complexType>

                             <xs:simpleContent>

                                  <xs:extension base="xs:integer">

                                   <xs:attribute name="Author" type="xs:string"/>

                                  </xs:extension>

                                </xs:simpleContent>

                            </xs:complexType>

                        </xs:element>

                    </xs:sequence>

                </xs:complexType>

            </xs:element>

        </xs:sequence>

    </xs:complexType>

    </xs:element>

    </xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

我正在尝试用 C# 读取代码:


XmlSchemaSet schema = new XmlSchemaSet();

schema.Add("", "PATH_OF_XSD_FILE");

C# 中出现错误:


type 属性不能与 simpleType 或 complexType 一起出现。


慕村225694
浏览 138回答 1
1回答

HUH函数

根据 klaus Gutter 的说法,如果使用复杂类型,类型将是我们可以使用的类型base。检查以下代码:<xs:element name="Dest">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xs:complexType>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xs:sequence>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xs:element name="Id">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xs:complexType>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xs:simpleContent>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xs:extension base="xs:string">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xs:attribute name="Auther" type="xs:string" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xs:extension>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xs:simpleContent>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xs:complexType>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xs:element>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xs:sequence>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xs:complexType>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xs:element>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP