You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将自定义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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:40:32