一、定义序列化实体类
[System.Xml.Serialization.XmlRoot("Custome_Xml_Root_Name")] //自定义生成的Xml根目录名称
public class Customer
{
[System.Xml.Serialization.XmlAttribute("Customer_Xml_Root_Atrribute_Name")] //自定义生产的Xml根目录属性名称
public int ID { get; set; } [System.Xml.Serialization.XmlElement("Custom_FirstName")] //自定义生成的Xml属性名
public string FirstName { get; set; } [System.Xml.Serialization.XmlIgnore()] //不包括在生成的Xml中的属性
public string IgnoreName { get; set; }
}
二、序列化实体类
public class SerializeCustomer
{
public void Start()
{
Customer c = new Customer();
c.ID = ;
c.FirstName = "James";
c.IgnoreName = "Ignore"; //实体类值序列化到XML文件中
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(Customer));
System.IO.Stream s = System.IO.File.OpenWrite("Customer.xml");
xs.Serialize(s, c);
s.Close();
}
}
三、生成的Xml文件样式
<?xml version="1.0" encoding="utf-8" ?>
<Custome_Xml_Root_Name Customer_Xml_Root_Atrribute_Name="1">
<Custom_FirstName>James</Custom_FirstName>
</Custome_Xml_Root_Name>