转自:http://www.cnblogs.com/zsi/archive/2005/04/11/135665.html
要使用.NET进行对象的序列化,必须在解决方案中添加System.Xml的引用,并且在类文件中引入System.Xml.Serialization命名空间。这样就可以在文件中使用序列化所需要的各种特性了。
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
如果对XML Serialization缺少了解,请首先参考拙文:在.NET中实现对象序列化
![](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)
上面的例子包含了典型的XML中常见的各种元素:XML声明、XML根节点、XML节点、XML属性、XML集合。除XML声明外,在.NET中都有对应的特性用于定义这些元素。这些特性包括:XmlRootAttribute、XmlTypeAttribute、XmlElementAttribute、XmlAttributeAttribute、XmlArrayAttribute和XmlArrayItemAttribute。另外,还有两个常用的特性,XmlIgnoreAttribute用于标记在对象序列化时需要被忽略的部分,XmlIncludeAttribute用于标记在生成XML Schema时需要包括的类型。
如果没有显式地标记任何特性,那么默认类的特性为XmlTypeAttribute、类成员的特性为XmlElementAttribute,且名称为类或类成员的名称。例如:
![](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)
![](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)
序列化后的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)
可以看到,<Order>对应Order类,而<ID>和<OrderDate>分别对应Order类中的字段ID和OrderDate。另外,多了一个XML声明和两个XML命名空间。
XML声明是.NET自动添加的,但是encoding是在XmlTextWriter中指定的,如果不指定encoding,那么XML声明只有<?xml version="1.0"?>。我使用的是.NET 1.1,这个版本中只支持XML 1.0版本。另外,如果不指定encoding,那么默认的编码可能也是UTF8(没找到相关的资料)。
.NET默认为Order类添加了XMLSchema和XMLSchema-instance两个W3C的命名空间。该命名空间也可以自己指定,方法是使用XmlSerializer的另一个Serialize方法。
![](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)
要将类序列化为XML节点:
![](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/ExpandedBlockEnd.gif)
要将类序列化为XML根节点:
![](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/ExpandedBlockEnd.gif)
当在类中同时使用XmlRootAttribute、XmlTypeAttribute时,序列化文档中的类型以XmlRootAttribute为准:
![](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/ExpandedBlockEnd.gif)
要将类成员序列化为XML节点:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
要将类成员序列化为XML属性:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
要将类成员序列化为XML集合:
![](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/ExpandedBlockEnd.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.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/ExpandedBlockEnd.gif)
使用特性的一个好处是:可以在代码和序列化的文档中使用不同的编码规范。