XML文件部分:
<?
xml version="1.0" encoding="utf-8"
?>
< shiporder orderid ="889923"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation ="shiporder.xsd" >
< orderperson > George Bush </ orderperson >
< shipto >
< name > John Adams </ name >
< address > Oxford Street </ address >
< city > London </ city >
< country > UK </ country >
</ shipto >
< item >
< title > Empire Burlesque </ title >
< note > Special Edition </ note >
< quantity > 1 </ quantity >
< price > 10.90 </ price >
</ item >
< item >
< title > Hide your heart </ title >
< quantity > 1 </ quantity >
< price > 9.90 </ price >
</ item >
</ shiporder >
< shiporder orderid ="889923"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation ="shiporder.xsd" >
< orderperson > George Bush </ orderperson >
< shipto >
< name > John Adams </ name >
< address > Oxford Street </ address >
< city > London </ city >
< country > UK </ country >
</ shipto >
< item >
< title > Empire Burlesque </ title >
< note > Special Edition </ note >
< quantity > 1 </ quantity >
< price > 10.90 </ price >
</ item >
< item >
< title > Hide your heart </ title >
< quantity > 1 </ quantity >
< price > 9.90 </ price >
</ item >
</ shiporder >
XSD文件部分:
<?
xml version="1.0" encoding="iso-8859-1"
?>
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:element name ="shiporder" >
< xs:complexType >
< xs:sequence >
< xs:element name ="orderperson" type ="xs:string" />
< xs:element name ="shipto" >
< xs:complexType >
< xs:sequence >
< xs:element name ="name" type ="xs:string" />
< xs:element name ="address" type ="xs:string" />
< xs:element name ="city" type ="xs:string" />
< xs:element name ="country" type ="xs:string" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
< xs:element name ="item" maxOccurs ="unbounded" >
< xs:complexType >
< xs:sequence >
< xs:element name ="title" type ="xs:string" />
< xs:element name ="note" type ="xs:string" minOccurs ="0" />
< xs:element name ="quantity" type ="xs:positiveInteger" />
< xs:element name ="price" type ="xs:decimal" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:sequence >
< xs:attribute name ="orderid" type ="xs:string" use ="required" />
</ xs:complexType >
</ xs:element >
</ xs:schema >
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" >
< xs:element name ="shiporder" >
< xs:complexType >
< xs:sequence >
< xs:element name ="orderperson" type ="xs:string" />
< xs:element name ="shipto" >
< xs:complexType >
< xs:sequence >
< xs:element name ="name" type ="xs:string" />
< xs:element name ="address" type ="xs:string" />
< xs:element name ="city" type ="xs:string" />
< xs:element name ="country" type ="xs:string" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
< xs:element name ="item" maxOccurs ="unbounded" >
< xs:complexType >
< xs:sequence >
< xs:element name ="title" type ="xs:string" />
< xs:element name ="note" type ="xs:string" minOccurs ="0" />
< xs:element name ="quantity" type ="xs:positiveInteger" />
< xs:element name ="price" type ="xs:decimal" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:sequence >
< xs:attribute name ="orderid" type ="xs:string" use ="required" />
</ xs:complexType >
</ xs:element >
</ xs:schema >
.CS文件部分:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Xml.Serialization;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true ;
settings.IgnoreComments = true ;
NameTable nt = new NameTable();
string link = nt.Add( " shipto " );
settings.NameTable = nt;
// 验证
string strSchemaFileSrc = base .ResolveUrl(Path.Combine(Request.PhysicalApplicationPath, " shiporder.xsd " ));
settings.Schemas.Add( null , XmlReader.Create(strSchemaFileSrc));
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);
// 序列化工厂类
XmlSerializerFactory factory = new XmlSerializerFactory();
string strDataFileSrc = base .ResolveUrl(Path.Combine(Request.PhysicalApplicationPath, " shiporder.xml " ));
using (XmlReader reader = XmlReader.Create(strDataFileSrc, settings))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && String.Compare(link, reader.LocalName, true ) == 0 )
{
XmlSerializer xs = factory.CreateSerializer( typeof (shipto));
// 获取到对象,对象中已包含xml中的数据
shipto l = (shipto)xs.Deserialize(reader.ReadSubtree());
}
}
}
}
private void settings_ValidationEventHandler( object sender, System.Xml.Schema.ValidationEventArgs e)
{
throw new Exception( " 数据文件: " + "" + " 格式不正确! [ " + e.Message + " ] " );
}
}
public class shipto
{
private string _name;
private string _address;
private string _city;
private string _country;
public string name
{
get { return _name; }
set { _name = value; }
}
public string address
{
get { return _address; }
set { _address = value; }
}
public string city
{
get { return _city; }
set { _city = value; }
}
public string country
{
get { return _country; }
set { _country = value; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Xml.Serialization;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true ;
settings.IgnoreComments = true ;
NameTable nt = new NameTable();
string link = nt.Add( " shipto " );
settings.NameTable = nt;
// 验证
string strSchemaFileSrc = base .ResolveUrl(Path.Combine(Request.PhysicalApplicationPath, " shiporder.xsd " ));
settings.Schemas.Add( null , XmlReader.Create(strSchemaFileSrc));
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);
// 序列化工厂类
XmlSerializerFactory factory = new XmlSerializerFactory();
string strDataFileSrc = base .ResolveUrl(Path.Combine(Request.PhysicalApplicationPath, " shiporder.xml " ));
using (XmlReader reader = XmlReader.Create(strDataFileSrc, settings))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && String.Compare(link, reader.LocalName, true ) == 0 )
{
XmlSerializer xs = factory.CreateSerializer( typeof (shipto));
// 获取到对象,对象中已包含xml中的数据
shipto l = (shipto)xs.Deserialize(reader.ReadSubtree());
}
}
}
}
private void settings_ValidationEventHandler( object sender, System.Xml.Schema.ValidationEventArgs e)
{
throw new Exception( " 数据文件: " + "" + " 格式不正确! [ " + e.Message + " ] " );
}
}
public class shipto
{
private string _name;
private string _address;
private string _city;
private string _country;
public string name
{
get { return _name; }
set { _name = value; }
}
public string address
{
get { return _address; }
set { _address = value; }
}
public string city
{
get { return _city; }
set { _city = value; }
}
public string country
{
get { return _country; }
set { _country = value; }
}
}
}