2010-05-04 74 views
1

這是我製作的一些dummy xml和dummy xml架構。XML Deserilzation爲什麼不在應該拋出異常

模式

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.domain.com" 
xmlns="http://www.domain.com" 
elementFormDefault="qualified"> 
    <xs:element name="vehicles"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="owner" minOccurs="1" maxOccurs="1"> 
      <xs:simpleType> 
      <xs:restriction base="xs:string"> 
       <xs:minLength value="2" /> 
       <xs:maxLength value="8" /> 
      </xs:restriction> 
      </xs:simpleType> 
     </xs:element> 
     <xs:element name="Car" minOccurs="1" maxOccurs="1"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="Information" type="CarInfo" minOccurs="0" maxOccurs="unbounded" /> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="Truck"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="Information" type="CarInfo" minOccurs="0" maxOccurs="unbounded"/> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="SUV"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="Information" type="CarInfo" minOccurs="0" maxOccurs="unbounded" /> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
    <xs:complexType name="CarInfo"> 
    <xs:sequence> 
     <xs:element name="CarName"> 
     <xs:simpleType> 
      <xs:restriction base="xs:string"> 
      <xs:minLength value="1"/> 
      <xs:maxLength value="50"/> 
      </xs:restriction> 
     </xs:simpleType> 
     </xs:element> 
     <xs:element name="CarPassword"> 
     <xs:simpleType> 
      <xs:restriction base="xs:string"> 
      <xs:minLength value="6"/> 
      <xs:maxLength value="50"/> 
      </xs:restriction> 
     </xs:simpleType> 
     </xs:element> 
     <xs:element name="CarEmail"> 
     <xs:simpleType> 
      <xs:restriction base="xs:string"> 
      <xs:pattern value="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/> 
      </xs:restriction> 
     </xs:simpleType> 
     </xs:element> 
    </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

XML樣本

<?xml version="1.0" encoding="utf-8" ?> 
<vehicles> 
    <owner>Car</owner> 
    <Car> 
    <Information> 
     <CarName>Bob</CarName> 
     <CarPassword>123456</CarPassword> 
     <CarEmail>[email protected]</CarEmail> 
    </Information> 
    <Information> 
     <CarName>Bob2</CarName> 
     <CarPassword>123456</CarPassword> 
     <CarEmail>[email protected]</CarEmail> 
    </Information> 
    </Car> 
    <Truck> 
    <Information> 
     <CarName>Jim</CarName> 
     <CarPassword>123456</CarPassword> 
     <CarEmail>[email protected]</CarEmail> 
    </Information> 
    <Information> 
     <CarName>Jim2</CarName> 
     <CarPassword>123456</CarPassword> 
     <CarEmail>[email protected]</CarEmail> 
    </Information> 
    </Truck> 
    <SUV> 
    <Information> 
     <CarName>Jane</CarName> 
     <CarPassword>123456</CarPassword> 
     <CarEmail>[email protected]</CarEmail> 
    </Information> 
    <Information> 
     <CarName>Jane</CarName> 
     <CarPassword>123456</CarPassword> 
     <CarEmail>[email protected]</CarEmail> 
    </Information> 
    </SUV> 
</vehicles> 

序列化類

using System; 
using System.Collections.Generic; 
using System.Xml; 
using System.Xml.Serialization; 

[XmlRoot("vehicles")] 
public class MyClass 
{ 
    public MyClass() 
    { 
     Cars = new List<Information>(); 
     Trucks = new List<Information>(); 
     SUVs = new List<Information>(); 
    } 



    [XmlElement(ElementName = "owner")] 
    public string Owner { get; set; } 

    [XmlElement("Car")] 
    public List<Information> Cars { get; set; } 

    [XmlElement("Truck")] 
    public List<Information> Trucks { get; set; } 

    [XmlElement("SUV")] 
    public List<Information> SUVs { get; set; } 

} 

public class CarInfo 
{ 
    public CarInfo() 
    { 
     Info = new List<Information>(); 
    } 

    [XmlElement("Information")] 
    public List<Information> Info { get; set; } 
} 

public class Information 
{ 
    [XmlElement(ElementName = "CarName")] 
    public string CarName { get; set; } 

    [XmlElement("CarPassword")] 
    public string CarPassword { get; set; } 

    [XmlElement("CarEmail")] 
    public string CarEmail { get; set; } 
} 

現在,我想這應該所有驗證。如果不是假設它是寫作的,因爲我的真實文件確實有效,這就是這個虛擬文件的基礎。現在我的問題是這樣的。我想盡我所能從我的模式執行。如「所有者」標籤必須是第一個元素,並且應顯示一次並且僅顯示一次(minOccurs =「1」maxOccurs =「1」)。

現在,我可以從我的虛擬XML文件中刪除所有者元素和deseriliaze它,它會繼續它的快樂方式,並將其轉換爲對象,將只是把這個屬性爲空。我不希望我想讓它拋出一個異常,或者說這與所期望的相符。

我不希望有驗證之類的東西,一旦反序列化。 <car></car>標籤也是如此,即使沒有任何信息,我也希望它始終顯示,但我也可以刪除它,它會很滿意。

那麼什麼標籤我必須補充,使我的序列化類知道需要這些東西,如果他們沒有找到拋出異常。

回答

1

API是在做什麼[de]序列化?如果這是內在的東西像ASMX,那麼有可能是沒有什麼可以做(簡稱實施IXmlSerializable,這是可怕的),但是如果你有過XmlReader/XmlWriter創建直接控制(例如,從一個文件或網絡套接字),您可以讓讀寫器爲您進行驗證,like so

+0

雅這就是我用了我第一次讀入一個XMLReader得到使用的模式驗證。它似乎仍然可以反序列化所有的xml。我希望一旦找到無效部分就會死掉。 – chobo2 2010-05-04 06:47:48

0

我不知道的元素,但可以肯定使屬性「所有者」,並使其需要的作品。你可以在所有者屬性上使用[XmlAttribute]。

+0

你能告訴我一個例子嗎? – chobo2 2010-05-04 06:46:37

相關問題