MSDN上的定义:
XML 序列化将对象的公共字段和属性或者方法的参数和返回值转换(序列化)为符合特定 XML 架构定义语言 (XSD) 文档的 XML 流。XML 序列化生成强类型的类,并为存储或传输目的将其公共属性和字段转换为序列格式(在此情况下为 XML)简单的说就是将一个对象转换成XML流或是文件的过程。
注意事项:要序列化的类必须有一个默认的构造器,不能序列化类中的私有变量或方法。
下面以一个实例来说明。
这个是要序列化的类。
public
class
XmlSerObject
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public XmlSerObject()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DateTime m_datetime;
public DateTime CurrentDate
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_datetime; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this.m_datetime = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string m_name;
public string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.m_name; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this.m_name = value; }
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public XmlSerObject(string name, DateTime current)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_name = name;
this.m_datetime = current;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
这个是我写的一个方法用来序列化这个类并将数据保存到一个xml文件中。
/**/
/// <summary>
/// 序列化一个对象
/// </summary>
/// <param name="fileName">要存放的文件名(包括路径)</param>
/// <param name="xmlobj">被序列化的对象</param>
public
static
void
SerObject(
string
fileName, Object xmlobj)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//得到被序列化的类型
Type type = xmlobj.GetType();
XmlSerializer sz = new XmlSerializer(type);
//开始序列化
sz.Serialize(stream, xmlobj);
}
}
通过这个方法就可以生成一个XML文件
private
void
btnXml_Click(
object
sender, EventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//创建一个对象。
XmlSerObject serobj = new XmlSerObject("fastyou", DateTime.Now);
//XmlSerialize这个是SerObject这个方法所在的类名。
XmlSerialize.SerObject(@"c:\fastyou.xml", serobj);
}
接着我们来打开这个xml文件来看看结果。
<?
xml version="1.0"
?>
<
XmlSerObject
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
>
<
CurrentDate
>
2006-11-14T00:53:09.5731779+08:00
</
CurrentDate
>
<
Name
>
fastyou
</
Name
>
</
XmlSerObject
>
这个就是我们序列化后看到的结果。如果你在要序列化的类名加上[XmlRoot("Fastyou")]这个Attribute的话那么XML文件里的XmlSerObject将会变成Fastyou.^_^.如果在类的Name属性[XmlAttribute("Name")]这个Attribute的话结果又会有变化请看下面Name是不是换了位置呢。
<?
xml version="1.0"
?>
<
XmlSerObject
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
Name
="fastyou"
>
<
CurrentDate
>
2006-11-14T00:57:09.0278354+08:00
</
CurrentDate
>
</
XmlSerObject
>
如果你将序列化的方法改成下面的形式就会得到这个xml文件的内容的一个字符串
/**/
/// <summary>
/// 序列化一个对象
/// </summary>
/// <param name="fileName">要存放的文件名(包括路径)</param>
/// <param name="xmlobj">被序列化的对象</param>
public
static
string
SerObject(
string
fileName, Object xmlobj)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
Type type = xmlobj.GetType();
XmlSerializer sz = new XmlSerializer(type);
sz.Serialize(tw, xmlobj);
tw.Close();
return sb.ToString();
}
这是一个简单的XML序列的实例。有时间再写更深入一点的有关XML序列化的文章。
调试环境:xp sp2,vs2005