这是XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="xmlData">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="ident" type="xs:int" />
<xs:element name="stock" type="xs:int" />
<xs:element name="pricewotax" type="xs:double" />
<xs:element name="discountpercent" type="xs:double" />
<xs:element minOccurs="0" name="pricebyquantity">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="quantity" type="xs:int" />
<xs:element name="pricewotax" type="xs:double" />
<xs:element name="discountpercent" type="xs:double" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="date" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
下面是可序列化类:
namespace StockInfo {
using System.Xml.Serialization;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class xmlData {
private xmlDataTable[] tableField;
private string dateField;
[System.Xml.Serialization.XmlElementAttribute("Table")]
public xmlDataTable[] Table {
get {
return this.tableField;
}
set {
this.tableField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string date {
get {
return this.dateField;
}
set {
this.dateField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class xmlDataTable {
private int identField;
private int stockField;
private double pricewotaxField;
private double discountpercentField;
private xmlDataTableTable[] pricebyquantityField;
public int ident {
get {
return this.identField;
}
set {
this.identField = value;
}
}
public int stock {
get {
return this.stockField;
}
set {
this.stockField = value;
}
}
public double pricewotax {
get {
return this.pricewotaxField;
}
set {
this.pricewotaxField = value;
}
}
public double discountpercent {
get {
return this.discountpercentField;
}
set {
this.discountpercentField = value;
}
}
[System.Xml.Serialization.XmlArrayItemAttribute("Table", IsNullable=false)]
public xmlDataTableTable[] pricebyquantity {
get {
return this.pricebyquantityField;
}
set {
this.pricebyquantityField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class xmlDataTableTable {
private int quantityField;
private double pricewotaxField;
private double discountpercentField;
public int quantity {
get {
return this.quantityField;
}
set {
this.quantityField = value;
}
}
public double pricewotax {
get {
return this.pricewotaxField;
}
set {
this.pricewotaxField = value;
}
}
public double discountpercent {
get {
return this.discountpercentField;
}
set {
this.discountpercentField = value;
}
}
}
}
一个警告:反序列化可能不是解析20MB文件最有效的方法。xmlreader可能是最快的方法,但这意味着要手动完成任务。