·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> (转)xml序列化

(转)xml序列化

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

(转)xml序列化

在.NET Framework 中提供两种串行化方法,一种是二进制法,另一种是xml串行化。

序列化是将对象状态转换为可保持或传输的格式的过程,xml序列化是将对象的公共字段和属性序列化为xml流。由于xml是一个开放式标准,因此对于通过web共享数据而言,这是一个很好的选择。

将对象序列化,可以将对象状态永久保存在存储媒体上,以便可以在以后创建更精确的副本;同时,通过值可以将对象一个应用程序域发送到另一个应用程序域中。

xml序列化中最主要的类是XmlSerializer类,它的最重要的方法是Serializer和Deserializer方法。使用xmlSerializer可以将以下几项序列化:公共类的公共读/写属性,字段; 实现ICollection或IEnumerable的类;xmlElement对象;xmlNode对象;Dataset对象。下面以一个很简单的例子演示一下:

首先导入命名空间:using System.Xml.Serialization;和using System.IO;并且创建一个student类:

//创建一个公共类 public class Student { public string studentName = null; public int studentAge = 0; PRivate decimal money = 0;

public decimal Money { get { return money; } set { money = value; } }

}

然后在主方法里序列化:

static void Main()

{

//1.创建对象并赋值它的公共字段和属性。

Student st = new Student(