这是我制作的一些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>
标签也是如此,即使没有任何信息,我也希望它始终显示,但我也可以删除它,它会很满意。
那么什么标签我必须补充,使我的序列化类知道需要这些东西,如果他们没有找到抛出异常。
雅这就是我用了我第一次读入一个XMLReader得到使用的模式验证。它似乎仍然可以反序列化所有的xml。我希望一旦找到无效部分就会死掉。 – chobo2 2010-05-04 06:47:48