XML转换为对象操作类
1 //XML转换为对象操作类 2 3 //一,XML与Object转换类 4 5 using System.IO; 6 using System.Runtime.Serialization.Formatters.Binary; 7 using System.Text; 8 using System.Xml; 9 using System.Xml.Serialization; 10 11 namespace WebApplication1 12 { 13 public sealed class XMLSerilizable 14 { 15 /// <summary> 16 /// 将object对象序列化成XML 17 /// </summary> 18 /// <typeparam name="T"></typeparam> 19 /// <param name="encoding"></param> 20 /// <returns></returns> 21 public static string ObjectToXML<T>(T t, Encoding encoding) 22 { 23 XmlSerializer ser = new XmlSerializer(t.GetType()); 24 using (MemoryStream mem = new MemoryStream()) 25 { 26 using (XmlTextWriter writer = new XmlTextWriter(mem, encoding)) 27 { 28 XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 29 ns.Add("", ""); 30 ser.Serialize(writer, t, ns); 31 return encoding.GetString(mem.ToArray()).Trim(); 32 } 33 } 34 } 35 36 /// <summary> 37 /// 将XML反序列化成对象 38 /// </summary> 39 /// <typeparam name="T"></typeparam> 40 /// <param name="source"></param> 41 /// <param name="encoding"></param> 42 /// <returns></returns> 43 public static T XMLToObject<T>(string source, Encoding encoding) 44 { 45 XmlSerializer mySerializer = new XmlSerializer(typeof(T)); 46 using (MemoryStream stream = new MemoryStream(encoding.GetBytes(source))) 47 { 48 return (T)mySerializer.Deserialize(stream); 49 } 50 } 51 52 /// <summary> 53 /// 二进制方式序列化对象 54 /// </summary> 55 /// <param name="testUser"></param> 56 public static string Serialize<T>(T obj) 57 { 58 MemoryStream ms = new MemoryStream(); 59 BinaryFormatter formatter = new BinaryFormatter(); 60 formatter.Serialize(ms, obj); 61 return ms.ToString(); 62 } 63 64 /// <summary> 65 /// 二进制方式反序列化对象 66 /// </summary> 67 /// <returns></returns> 68 public static T DeSerialize<T>(string str) where T : class 69 { 70 MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)); 71 BinaryFormatter formatter = new BinaryFormatter(); 72 T t = formatter.Deserialize(ms) as T; 73 return t; 74 } 75 76 } 77 } 78 79 80 81 //二,测试类 82 83 using System; 84 using System.Xml.Serialization; 85 86 namespace WebApplication1 87 { 88 [Serializable] 89 [XmlRoot("Result")] 90 public class XMLClass 91 { 92 private string rtnValue; 93 [XmlElement("Value")] 94 public string RtnValue 95 { 96 get { return rtnValue; } 97 set { rtnValue = value; } 98 } 99 private string rtnKey; 100 [XmlElement("Key")] 101 public string RtnKey 102 { 103 get { return rtnKey; } 104 set { rtnKey = value; } 105 } 106 } 107 } 108 109 //三,调用 110 111 112 113 using System; 114 using System.Text; 115 116 namespace WebApplication1 117 { 118 public partial class WebForm1 : System.Web.UI.Page 119 { 120 protected void Page_Load(object sender, EventArgs e) 121 { 122 XMLClass xc = new XMLClass(); 123 xc.RtnKey = "TestXML"; 124 xc.RtnValue = "ObjectToXML"; 125 string xml = XMLSerilizable.ObjectToXML<XMLClass>(xc,System.Text.Encoding.UTF8); 126 127 StringBuilder strXML = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 128 strXML.Append("<Result>"); 129 strXML.AppendFormat("<Key>{0}</Key>","TestXML"); 130 strXML.AppendFormat("<Value>{0}</Value>","XMLToObject"); 131 strXML.Append("</Result>"); 132 133 XMLClass xClass = XMLSerilizable.XMLToObject<XMLClass>(strXML.ToString(),System.Text.Encoding.UTF8); 134 } 135 } 136 }