利用XMLSerializer将对象串行化到XML

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0cm; margin-bottom:.0001pt; text-align:center; mso-pagination:none; tab-stops:center 207.65pt right 415.3pt; layout-grid-mode:char; border:none; mso-border-bottom-alt:solid windowtext .75pt; padding:0cm; mso-padding-alt:0cm 0cm 1.0pt 0cm; font-size:9.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:#004D9F; mso-text-animation:none; text-decoration:none; text-underline:none; text-decoration:none; text-line-through:none;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} span.tx1 {mso-style-name:tx1; font-weight:bold;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:1.0cm 1.0cm 1.0cm 1.0cm; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

利用XMLSerializer将对象串行化到XML

微软已经意识到串行化数据的重要性,因此在.NET框架中包含了命名空间System.Runtime.SerializationSystem.Xml.Serialization以提供串行化功能,为用户自己编写串行化方法提供了一个框架。System.Xml.Serialization命名空间提供了将一个对象串行化为XML格式的基本方法。下面我们来看看如何使用这种方法。

XML的魅力 
   
串行化XML是指为了方便存储或传输,把一个对象的公共的域和属性保存为一种串行格式(这里是XML格式)的过程。非串行化则是使用串行的状态信息将对象从串行XML状态还原成原始状态的过程。因此,可以把串行化看作是将对象的状态保存到流或缓冲区中的一种方法。

串行化的目的是数据存储和数据转换。数据存储指的是在用户会话时保存数据。当应用程序关闭时,数据被保存(串行化),而当用户回来时,数据又被重新加载(非串行化)。数据转换指将数据变换成能被另一个系统识别的格式。使用串行化和XML,可以很方便的进行数据转换。

对象中的数据可以是类、方法、属性、私有类型、数组,在System.Xml.XmlElementSystem.Xml.XmlAttribute对象中,它甚至可以是内嵌的XML

System.Xml.Serialization命名空间中的关键类是XmlSerializer。当然在该命名空间中还包括有关XML其他方面以及SOAP相关的其他类,不过我们的重点是XmlSerializer类。

XmlSerializer
    XmlSerializer
类提供了把对象串行化为XML文件及把XML文档非串行化为对象的方法。它还能让用户指定对象如何转化为XML。可以把将被串行化的对象的类型作为类构造函数的参数。下面的C# 代码说明了构造函数的用法。

    XmlSerializer ser = new XmlSerializer(typeof(objectToSerialize));   

实际的串行化过程在XmlSerializer类的Serialize方法中实现。该方法允许在串行化过程中调用TextWriterStreamXmlWriter对象。下面的例子代码说明了如何调用该方法。在本例中一个对象被串行化保存到本地磁盘的一个文件当中。例子中首先是类声明,后面紧接着是串行化源代码。

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.Xml.Serialization;

 

namespace BuilderSerialization

{

    public class Address

    {

       public Address() { }

       public string Address1;

       public string Address2;

       public string City;

       public string State;

       public string Zip;

       public string Country;

    }

}

 

namespace BuilderSerialization

{

    public class Author

    {

       public Author() { }

       public string FirstName;

       public string MiddleName;

       public string LastName;

       public string Title;

       public string Gender;

       public Address AddressObject;

    }

}

 

namespace BuilderSerialization

{

    public class Book

    {

       public Book() { }

       public string Title;

       public Author AuthorObject;

       public string ISBN;

       public double RetailPrice;

       public string Publisher;

    }

}

 

namespace BuilderSerialization

{

    class TestClass

    {

       static void Main(string[] args)

       {

           Book BookObject = new Book();

 

           XmlSerializer ser = new XmlSerializer(typeof(Book));

           TextWriter writer = new StreamWriter("booktest.xml");

 

           BookObject.Title = "Practical LotusScript";

           BookObject.ISBN = "1884777767 ";

           BookObject.Publisher = "Manning Publications";

           BookObject.RetailPrice = 43.95;

 

           BookObject.AuthorObject = new Author();

           BookObject.AuthorObject.FirstName = "Tony";

           BookObject.AuthorObject.LastName = "Patton";

           BookObject.AuthorObject.Gender = "Male";

 

           BookObject.AuthorObject.AddressObject = new Address();

           BookObject.AuthorObject.AddressObject.Address1 = "1 Main Street";

           BookObject.AuthorObject.AddressObject.City = "Anywhere";

           BookObject.AuthorObject.AddressObject.State = "KY";

           BookObject.AuthorObject.AddressObject.Zip = "40000";

           BookObject.AuthorObject.AddressObject.Country = "USA";

 

           ser.Serialize(writer, BookObject);

 

           writer.Close();

       }

    }

}

上面的代码把三个对象变为一个对象,因此在串行化过程中产生一个XML文件。以下是例子程序产生的XML文档:

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Title>Practical LotusScript</Title>
<AuthorObject>
<FirstName>Tony</FirstName>
<LastName>Patton</LastName>
<Gender>Male</Gender>
<AddressObject>
<Address1>1 Main Street</Address1>
<City>Anywhere</City>
<State>KY</State>
<Zip>40000</Zip>
<Country>USA</Country>
</AddressObject>
</AuthorObject>
<ISBN>1884777767 </ISBN>
<RetailPrice>43.95</RetailPrice>
<Publisher>Manning Publications</Publisher>
</Book>

注意串行化过程也能处理对象数据的嵌套。数据被转换成可识别的格式,方便了数据重载(非串行化)以及向另一个系统的数据传输。在数据传输过程时,接收方系统需要知道XML文件的格式(如果预先不知道的话)。因此需要提供一个XML schema文件。.NET框架中的XSD.exe工具可以为串行化XML生成一个schema文件。

串行化过程生成标准的XML文件,数据成员转换为XML元素。不过,并非所有的数据成员都变成元素,可以通过在类代码中添加一些标记来控制输出的XML文件。这样,数据成员可以变换为XML属性而非元素,也可以简单的被忽略掉。以下是相应的C# 代码:

public class Book

    {

       public Book() { }

       public string Title;

       public Author AuthorObject;

       [System.Xml.Serialization.XmlAttribute()]

       public string ISBN;

       [System.Xml.Serialization.

           XmlIgnore()]

       public double RetailPrice;

       public string Publisher;

    }

上面仅是稍微提了两种标记符号。请查阅.NET文档以获得完整的标记符号。

客户端的反序列化

将序列化后的XML文件传输到客户端,在客户端进行反序列化。

客户端同样需要建立Book类和其他两个类:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.Xml.Serialization;

 

namespace BuilderSerialization

{

    public class Address

    {

       public Address() { }

       public string Address1;

       public string Address2;

       public string City;

       public string State;

       public string Zip;

       public string Country;

    }

}

 

namespace BuilderSerialization

{

    public class Author

    {

       public Author() { }

       public string FirstName;

       public string MiddleName;

       public string LastName;

       public string Title;

       public string Gender;

       public Address AddressObject;

    }

}

 

namespace BuilderSerialization

{

    public class Book

    {

       public Book() { }

       public string Title;

       public Author AuthorObject;

       [System.Xml.Serialization.XmlAttribute()]

       public string ISBN;

       [System.Xml.Serialization.

           XmlIgnore()]

       public double RetailPrice;

       public string Publisher;

    }

}

非串行化数据通过调用XmlSerializer类的Deserialize方法可以方便地实现:

namespace BuilderSerialization

{

    class TestClass

    {

       static void Main(string[] args)

       {         

           XmlSerializer ser2 = new XmlSerializer(typeof(Book));

           System.IO.FileStream fs = new

              System.IO.FileStream(@"F:/my files/project/产品说明/系统流程图/我的文档/讨论/booktest.xml",

              FileMode.Open);

           System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(fs);

           Book BookObject2 = (Book)(ser2.Deserialize(reader));

 

           Console.WriteLine(BookObject2.Publisher);

       }

 

    }

}

结果显示:Manning Publications

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值