一个可序列化的C#对象,如何转成一个XML格式的文件或字符串【转】

原文:http://blog.csdn.net/otong/article/details/7894059

序列化或反序列化成一个字符串:

方法一:

序列化:

[csharp] view plain copy
  1. public static string XMLSerialize<T>(T entity)  
  2.        {  
  3.            StringBuilder buffer = new StringBuilder();  
  4.              
  5.            XmlSerializer serializer = new XmlSerializer(typeof(T));  
  6.            using (TextWriter writer = new StringWriter(buffer))  
  7.            {  
  8.                serializer.Serialize(writer, entity);  
  9.            }  
  10.            
  11.            return buffer.ToString();  
  12.              
  13.        }  


反序列化:

[csharp] view plain copy
  1. public static T DeXMLSerialize<T>(string xmlString)  
  2.         {  
  3.             T cloneObject = default(T);  
  4.   
  5.             StringBuilder buffer = new StringBuilder();  
  6.             buffer.Append(xmlString);  
  7.   
  8.             XmlSerializer serializer = new XmlSerializer(typeof(T));  
  9.   
  10.             using (TextReader reader = new StringReader(buffer.ToString()))  
  11.             {  
  12.                 Object obj = serializer.Deserialize(reader);  
  13.                 cloneObject = (T)obj;  
  14.             }  
  15.   
  16.             return cloneObject;  
  17.         }  

方法二:

[csharp] view plain copy
  1. /// <summary>  
  2.     /// 对象序列化成 XML String  
  3.     /// </summary>  
  4.     public static string XmlSerialize<T>(T obj)  
  5.     {  
  6.         string xmlString = string.Empty;  
  7.         XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));  
  8.         using (MemoryStream ms = new MemoryStream())  
  9.         {  
  10.             xmlSerializer.Serialize(ms, obj);  
  11.             xmlString = Encoding.UTF8.GetString(ms.ToArray());  
  12.         }  
  13.         return xmlString;  
  14.     }  
  15.   
  16.     /// <summary>  
  17.     /// XML String 反序列化成对象  
  18.     /// </summary>  
  19.     public static T XmlDeserialize<T>(string xmlString)  
  20.     {  
  21.         T t = default(T);  
  22.         XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));  
  23.         using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))  
  24.         {  
  25.             using (XmlReader xmlReader = XmlReader.Create(xmlStream))  
  26.             {  
  27.                 Object obj = xmlSerializer.Deserialize(xmlReader);  
  28.                 t = (T)obj;  
  29.             }  
  30.         }  
  31.         return t;  
  32.     }  

如果要序列化到一个文件,则使用FileStream就可以了

如:

string strFile = @"c:/book.xml";
            using (FileStream fs = new FileStream(strFile, FileMode.Create))
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                formatter.Serialize(fs, entity);
            }

附:其实还可以直接用SringWriter

StringBuilder sb = new StringBuilder();
System.IO.StringWriter writer = new System.IO.StringWriter(sb);

XmlSerializer formatter = new XmlSerializer(typeof(T));
formatter .Serialize(writer,entity );

posted @ 2016-12-15 10:45  jimcsharp  阅读(5541)  评论(0编辑  收藏  举报