转自:http://www.cnblogs.com/zsi/archive/2005/04/07/132850.html
自从有了XML以后,跨系统的数据交换变得越来越简单。.NET更把XML做为底层数据交换的基础,并在.NET框架中提供了强大的类库以增强XML的编程能力。这些类包含在System.Xml命名空间中。在跨系统的数据交换中,对象的序列化与反序列化是重中之重。.NET为此专门提供了一个System.Xml.Serialization命名空间用于对象的序列化。
要了解XML的序列化可首先参考微软的介绍:
● Introducing XML Serialization
● XML and SOAP Serialization
● Roadmap for XML Serialization in the .NET Framework
.NET通过使用特性Attribute控制对象的序列化。例如:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
被.NET序列化后的XML是:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
XmlType:可以应用于声明为Public的类、结构、枚举和接口。它定义了Xml类型的名字和命名空间。
XmlElement:它定义了要序列化为XML元素的字段或属性。字段或属性必须声明为Public。
XmlArray:它定义了要序列化为XML集合的字段或属性。字段或属性必须声明为Public。
XmlArrayItem:定义了可以添加到集合中的派生类型。
.NET还提供了其它的一些Attribute。全部的Attribut特性可以通过下面地址查看:
● Attributes That Control XML Serialization
如何使用这些Attribute,请查看:
● Controlling XML Serialization Using Attributes
有了这些Attribute,我们可以很容易的将对象序列化,但是由谁来完成呢?那就需要了解一下XmlSerializer了。ASP.NET 就是使用 XmlSerializer 类对 XML Web services 消息进行编码的。
关于XmlSerializer 类请查看:
● XmlSerializer 类
● XmlSerializer.Serialize 方法
XmlSerializer的使用也非常简单:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
在使用XmlSerializer之前,可以先参考一下:
●XmlSerializer 常见问题疑难解答