2014-05-01 38 views
1

我想在編輯一些XML文件時使用Visual Studio的架構驗證。這些文件包含要讀取的序列化對象DataContractSerializeranyType元素的架構驗證

<?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' 

我試圖編輯XSD文件對於「http://schemas.microsoft.com/2003/10/Serialization/Arrays」命名空間,但到目前爲止我還沒有能夠消除這個錯誤。這是XSD文件作爲Visual Studio生成它。

<?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> 
+0

您是否可以包含導入的模式? http://schemas.datacontract.org/2004/07/MyLibrary – helderdarocha

回答

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> 

由於兩個MyObjectMyOtherObjectAbstractObject推導,它們可被用作類型<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> 
+0

我似乎無法得到您的建議工作的最後一點。首先它說「'類型'屬性是無效的 - 值'q1:MyObject'根據其數據類型'http://www.w3.org/2001/XMLSchema:QName'無效 - 'q1'是未聲明的前綴「。如果我將前綴移動到'xs:element'上,那麼它會顯示「Type'http://schemas.datacontract.org/2004/07/MyLibrary:MyObject'沒有聲明」,儘管我已經聲明瞭它。 – HappyNomad

+0

這可能是一個語法錯誤(例如,您粘貼的代碼中存在一些語法錯誤 - 例如,不匹配的標記)。如果聲明瞭名稱空間和前綴,則沒有理由發生錯誤。這可能是由於其他一些問題。我添加了一個可以用作示例的實例。剪切並粘貼所有模式的代碼,並對其進行測試。那麼,在適應現實世界的問題之前,您至少可以從一個實例開始。 – helderdarocha

+0

我測試了你的完整例子,它可以很好地協同工作,就像你說的那樣。謝謝。看起來我太過於簡單地在原始問題中簡化了我的代碼。請參閱我的更新。 – HappyNomad