XML 自定义串行化
使用XML串行化就可以将类的实例转换为XML,反之亦然。开发人员经常需要进行XML穿行化。
程序清单中展示了一个串行化的实例。第一步是创建 Person 类的实例,并用进行初始化。然后利用XmlSerializer将Person 类的这个实例串行化。
自定义串行化。假设需要把某个类的一个共有属性转换为 XML 属性,那么上面的做法就无法满足要求了。为了达到这一目的,我们可以实现自定义的XML串行化。程序清单如下:
运行结果:
程序代码下载:XMLSerializationSample.rar
程序清单中展示了一个串行化的实例。第一步是创建 Person 类的实例,并用进行初始化。然后利用XmlSerializer将Person 类的这个实例串行化。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Person 的摘要说明
/// </summary>
public class Person
{
public Person()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string Name
{
set
{
_name = value;
}
get
{
return _name;
}
} private string _name;
public int Age
{
set
{
_age = value;
}
get
{
return _age;
}
} private int _age;
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Person 的摘要说明
/// </summary>
public class Person
{
public Person()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string Name
{
set
{
_name = value;
}
get
{
return _name;
}
} private string _name;
public int Age
{
set
{
_age = value;
}
get
{
return _age;
}
} private int _age;
}
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml.Serialization" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Person person = new Person();
person.Name = "☆聊ゾ聊☆";
person.Age = 23;
//Response.Clear();
//Response.ContentType = "text/xml";
XmlSerializer serializer = new XmlSerializer(person.GetType());
serializer.Serialize(Response.OutputStream, person);
}
</script>
运行后得到的结果:
<%@ Import Namespace="System.Xml.Serialization" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Person person = new Person();
person.Name = "☆聊ゾ聊☆";
person.Age = 23;
//Response.Clear();
//Response.ContentType = "text/xml";
XmlSerializer serializer = new XmlSerializer(person.GetType());
serializer.Serialize(Response.OutputStream, person);
}
</script>
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>☆聊ゾ聊☆</Name>
<Age>23</Age>
</Person>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>☆聊ゾ聊☆</Name>
<Age>23</Age>
</Person>
自定义串行化。假设需要把某个类的一个共有属性转换为 XML 属性,那么上面的做法就无法满足要求了。为了达到这一目的,我们可以实现自定义的XML串行化。程序清单如下:
using System;
using System.Xml.Serialization;
[XmlRoot("员工")]
public class Employee
{
public Employee()
{
}
[XmlAttribute("姓名")]
public string Name
{
set
{
_name = value;
}
get
{
return _name;
}
} private string _name;
[XmlAttribute("年龄")]
public int Age
{
set
{
_age = value;
}
get
{
return _age;
}
} private int _age;
[XmlElement("备注")]
public string Remarks
{
set
{
_remarks = value;
}
get
{
return _remarks;
}
} private string _remarks;
}
using System.Xml.Serialization;
[XmlRoot("员工")]
public class Employee
{
public Employee()
{
}
[XmlAttribute("姓名")]
public string Name
{
set
{
_name = value;
}
get
{
return _name;
}
} private string _name;
[XmlAttribute("年龄")]
public int Age
{
set
{
_age = value;
}
get
{
return _age;
}
} private int _age;
[XmlElement("备注")]
public string Remarks
{
set
{
_remarks = value;
}
get
{
return _remarks;
}
} private string _remarks;
}
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml.Serialization" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Employee employee = new Employee();
employee.Name = "☆聊ゾ聊☆";
employee.Age = 23;
employee.Remarks = "☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆";
//Response.Clear();
//Response.ContentType = "text/xml";
XmlSerializer serializer = new XmlSerializer(employee.GetType());
serializer.Serialize(Response.OutputStream, employee);
}
</script>
<%@ Import Namespace="System.Xml.Serialization" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Employee employee = new Employee();
employee.Name = "☆聊ゾ聊☆";
employee.Age = 23;
employee.Remarks = "☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆";
//Response.Clear();
//Response.ContentType = "text/xml";
XmlSerializer serializer = new XmlSerializer(employee.GetType());
serializer.Serialize(Response.OutputStream, employee);
}
</script>
运行结果:
<?xml version="1.0"?>
<员工 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 姓名="☆聊ゾ聊☆" 年龄="23">
<备注>☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆</备注>
</员工>
<员工 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 姓名="☆聊ゾ聊☆" 年龄="23">
<备注>☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆</备注>
</员工>
程序代码下载:XMLSerializationSample.rar