反序列化,从XML中取数据
2011-05-12 01:03
197 查看
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>
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>
.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; }
}
}
}
<?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>
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>
.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; }
}
}
}
相关文章推荐
- Android应用中使用XmlSerializer序列化XML数据的教程
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml)
- 【iOS开发-95】JSON反序列化、XML数据解析以及主线程中的UI更新等小细节
- wcf序列化大对象时报错:读取 XML 数据时,超出最大
- java 使用xom对象数据序列化为xml、反序列化、Preferences相关操作小案例
- unity Dictionary序列化和反序列化及XML本地数据存储
- 处理:SqlServer FOR XML 无法对节点 'NoName' 的数据进行序列化,因为它包含在 XML 中不允许出现的字符(0x0000)。
- Android序列化XML数据
- XML序列化数据对象(一)
- 使用XStream序列化、反序列化XML数据时遇到的各种问题
- InnerException 消息是“反序列化对象 属于类型 *** 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。(注意细节)
- 【黑马Android】(02)短信发送器/布局演示/android下单位/android下Junit/保存数据/android下权限/xml解析和序列化
- XML序列化数据对象(二)
- DateTime数据类型的XML序列化及时区问题
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 【C#】解决进行反序列化时出错:。InnerException 消息是“反序列化对象 属于类型 System.String 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。
- c语言环境下的xml数据的序列化的过程以机顶盒向手机APK发送数据为例
- 试用GOOGLE PROTOCOL BUFFERS( 比XML快20~100倍, 支持序列化数据 )
- .net2.0中WebService在序列化xml数据Generic字典类型的问题
- pc与web平台利用xml写一个序列化与反序列化用来存储数据