在编辑一些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中使用了"Create Schema“菜单选项来生成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>发布于 2014-05-02 09:21:18
我在这里做了几个假设,因为您没有提供诊断问题的所有文件。我将包括完整的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元素基于一个抽象类型,MyObject和MyOtherObject都是从这个抽象类型派生出来的。在下面的示例中,我将其命名为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>因为MyObject和MyOtherObject都是从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>https://stackoverflow.com/questions/23418565
复制相似问题