将自定义XSD添加到XmlSchemaSet时触发XmlSchemaException问题排查
XSD添加到XmlSchemaSet时抛出XmlSchemaException异常
你编写的XSD代码如下:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:complexType name="FunnyType"> <xsd:element name="Prueba1" type="xsd:string"/> <xsd:element name="Prueba2" type="xsd:int"/> </xsd:complexType> <xsd:element name="Funnys"> <xsd:complexType> <xsd:sequence> <xsd:element name="funny" type="FunnyType" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
将其添加到XmlSchemaSet时,抛出System.Xml.Schema.XmlSchemaException,提示“'http://www.w3.org/2001/XMLSchema:element'在此上下文中无效”。
问题原因
在XSD的xsd:complexType定义中,直接放置xsd:element不符合语法规范。元素必须被包裹在内容模型容器内,比如xsd:sequence、xsd:choice或xsd:all,用来指定元素的出现顺序、可选性等规则。你定义的FunnyType里直接写了两个xsd:element,没有用容器包裹,这就是报错的根源。
修正后的XSD代码
把FunnyType中的元素放到xsd:sequence(或其他合适的容器)中即可:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:complexType name="FunnyType"> <xsd:sequence> <xsd:element name="Prueba1" type="xsd:string"/> <xsd:element name="Prueba2" type="xsd:int"/> </xsd:sequence> </xsd:complexType> <xsd:element name="Funnys"> <xsd:complexType> <xsd:sequence> <xsd:element name="funny" type="FunnyType" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
内容的提问来源于stack exchange,提问作者DaniWein
相关产品推荐
相关产品推荐

