using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //序列化 Person person = new Person() { Name = "dnawo", Age = 100 }; string xml = Serialize(person); Console.WriteLine(xml); //反序列化 person = Deserialize<Person>(xml); Console.WriteLine(person.Name); Console.ReadKey(); } /// <summary> /// 序列化 /// </summary> /// <param name="o"></param> /// <returns></returns> public static string Serialize(object o) { return Serialize(o, new UTF8Encoding(false)); } /// <summary> /// 序列化 /// </summary> /// <param name="o"></param> /// <param name="encode"></param> /// <returns></returns> public static string Serialize(object o, Encoding encode) { string result = string.Empty; //方法1: using (MemoryStream output = new MemoryStream()) { XmlSerializer serializer = new XmlSerializer(o.GetType()); serializer.Serialize(output, o); result = encode.GetString(output.ToArray()); } //方法2: //using (MemoryStream output = new MemoryStream()) //{ // using (XmlTextWriter writer = new XmlTextWriter(output, encode)) // { // XmlSerializer serializer = new XmlSerializer(o.GetType()); // serializer.Serialize(writer, o); // } // result = encode.GetString(output.ToArray()); //} //方法3:可刪除xml聲明和默認命名空間(xmlns:xsi,xmlns:xsd) //XmlWriterSettings setting = new XmlWriterSettings(); //setting.Encoding = encode; //setting.OmitXmlDeclaration = true; //XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); //namespaces.Add("", ""); //using (MemoryStream output = new MemoryStream()) //{ // using (XmlWriter writer = XmlWriter.Create(output, setting)) // { // XmlSerializer serializer = new XmlSerializer(o.GetType()); // serializer.Serialize(writer, o, namespaces); // result = encode.GetString(output.ToArray()); // } //} return result; } /// <summary> /// 反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="str"></param> /// <returns></returns> public static T Deserialize<T>(string str) where T : class { return Deserialize<T>(str, new UTF8Encoding(false)); } /// <summary> /// 反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="str"></param> /// <param name="encode"></param> /// <returns></returns> public static T Deserialize<T>(string str, Encoding encode) where T : class { T result = null; //方法1: using (MemoryStream input = new MemoryStream(encode.GetBytes(str))) { XmlSerializer serializer = new XmlSerializer(typeof(T)); result = serializer.Deserialize(input) as T; } //方法2: //using (MemoryStream input = new MemoryStream(encode.GetBytes(str))) //{ // using (XmlTextReader reader = new XmlTextReader(input)) // { // XmlSerializer serializer = new XmlSerializer(typeof(T)); // result = serializer.Deserialize(reader) as T; // } //} //方法3: //using (MemoryStream input = new MemoryStream(encode.GetBytes(str))) //{ // using (XmlReader reader = XmlReader.Create(input)) // { // XmlSerializer serializer = new XmlSerializer(typeof(T)); // result = serializer.Deserialize(reader) as T; // } //} return result; } }
/// <summary> /// 測試類 /// </summary> [XmlRoot("per")] public class Person { [XmlElement("na")] public string Name { get; set; } [XmlElement("ag")] public int Age { get; set; } }
運行結果:測試
<?xml version="1.0" encoding="utf-8"?><per xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><na>dnawo</na><ag>100</ag></per>