anyType元素的模式验证

时间:2014-05-01 23:45:31

标签: xml visual-studio serialization xsd datacontractserializer

我正在尝试在编辑某些XML文件时使用Visual Studio的架构验证。这些文件包含DataContractSerializer要读取的序列化对象。

<?xml version="1.0" encoding="utf-8" ?>
<MyRoot xmlns="http://schemas.datacontract.org/2004/07/MyDomain"
        xmlns:lib="http://schemas.datacontract.org/2004/07/MyLibrary"
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <MyList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <a:anyType i:type="lib:MyObject">
            <lib:Identifier>my-identifier</lib:Identifier>
            <lib:MyProperty>my-property-value</lib:MyProperty>
        </a:anyType>
        <a:anyType i:type="lib:MyOtherObject">
            <lib:Identifier>my-identifier</lib:Identifier>
            <lib:MyOtherProperty>my-other-property-value</lib:MyOtherProperty>
        </a:anyType>
    </MyList>
</MyRoot>

我使用Visual Studio中的“创建架构”菜单选项生成XSD文件,但编辑器仍然显示<a:anyType元素的此错误:

This is an invalid xsi:type 'http://schemas.datacontract.org/2004/07/MyLibrary:MyObject'

我尝试编辑“http://schemas.microsoft.com/2003/10/Serialization/Arrays”命名空间的XSD文件,但到目前为止我还没能消除错误。这是Visual Studio生成的XSD文件。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import namespace="http://schemas.datacontract.org/2004/07/MyLibrary" />
    <xs:element name="anyType">
        <xs:complexType>
            <xs:sequence>
                <xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/MyLibrary" ref="q1:MyProperty" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:1)

我在这里做了几个假设,因为你没有提供所有文件来诊断问题。我将包含您可以使用和测试的完整XSD文件,并尝试适应您的问题。

我假设你有一个像这样的主模式,它定义了你实例的默认命名空间中的元素(我称之为DataContract.xsd):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns="http://schemas.datacontract.org/2004/07/MyDomain"
    targetNamespace="http://schemas.datacontract.org/2004/07/MyDomain"> 

    <xs:element name="MyRoot">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="MyList"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="MyList">
        <xs:complexType>
            <xs:sequence>
                <xs:any maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

根据您发布的数据,我还假设您的anyType元素基于一个抽象类型,MyObjectMyOtherObject都来自该抽象类型。我在下面的示例(AbstractObject)中将其称为MyLibrary.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns="http://schemas.datacontract.org/2004/07/MyLibrary"
    targetNamespace="http://schemas.datacontract.org/2004/07/MyLibrary"> 

    <xs:complexType name="AbstractObject" abstract="true">
        <xs:sequence>
            <xs:element name="Identifier" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="MyObject">
        <xs:complexContent>
            <xs:extension base="AbstractObject">
                <xs:sequence>
                    <xs:element name="MyProperty" type="xs:string"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="MyOtherObject">
        <xs:complexContent>
            <xs:extension base="AbstractObject">
                <xs:sequence>
                    <xs:element name="MyOtherProperty" type="xs:string"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>

要允许通过anyType在您的实例中使用这些类型,您可以将其声明为具有AbstractObject类型(或在您的实际MyLibrary架构中调用的超类型):

<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 
           attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:q1="http://schemas.datacontract.org/2004/07/MyLibrary">

    <xs:import namespace="http://schemas.datacontract.org/2004/07/MyLibrary" schemaLocation="MyLibrary.xsd"/>
    <xs:element name="anyType" type="q1:AbstractObject" />

</xs:schema>

由于MyObjectMyOtherObject都来自AbstractObject,因此可以将它们用作<anyType>的类型。以下实例将在此方案中验证:

<MyRoot 
    xmlns="http://schemas.datacontract.org/2004/07/MyDomain"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:lib="http://schemas.datacontract.org/2004/07/MyLibrary"
    i:schemaLocation="http://schemas.datacontract.org/2004/07/MyDomain DataContract.xsd
                      http://schemas.microsoft.com/2003/10/Serialization/Arrays SerializationArrays.xsd">

    <MyList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <a:anyType i:type="lib:MyObject">
            <lib:Identifier>my-identifier</lib:Identifier>
            <lib:MyProperty>my-property-value</lib:MyProperty>
        </a:anyType>
        <a:anyType i:type="lib:MyOtherObject">
            <lib:Identifier>my-identifier</lib:Identifier>
            <lib:MyOtherProperty>my-other-property-value</lib:MyOtherProperty>
        </a:anyType>
    </MyList>
</MyRoot>