1. 程式人生 > >把物件序列化為xml格式和反序列化

把物件序列化為xml格式和反序列化

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization;
namespace ParkingSqlHelper { public class XmlHelper { /// <summary> /// 序列化物件 /// </summary> /// <typeparam name="T">物件型別</typeparam> /// <param name="t">物件</param> /// <returns></returns> public static string Serialize<T>(T t) { MemoryStream ms; XmlWriter xw; XmlWriterSettings xws; XmlSerializerNamespaces ns; XmlSerializer xml;
ms = new MemoryStream(); ns = new XmlSerializerNamespaces(); ns.Add("", ""); //這樣就 去掉 attribute 裡面的 xmlns:xsi 和 xmlns:xsd xws = new XmlWriterSettings(); xws.Encoding = new UTF8Encoding(false); //設定編碼,不能用Encoding.UTF8,會導致帶有BOM標記 xws.Indent = true; //是否格式化文件。 xml = new XmlSerializer(typeof(T)); using (xw = XmlWriter.Create(ms, xws)) { //序列化物件 xml.Serialize(xw, t, ns); }
return xws.Encoding.GetString(ms.ToArray()).Trim(); }
/// <summary> /// 反序列化為物件 /// </summary> /// <typeparam name="T">物件型別</typeparam> /// <param name="s">物件序列化後的</param> /// <returns></returns> public static T Deserialize<T>(string s) { T obj; XmlReader xr; XmlSerializer xs;
xr = XmlReader.Create(new StringReader(s)); xs = new XmlSerializer(typeof(T)); obj = (T)xs.Deserialize(xr);
return obj; }
public static void SaveObjectToFile<T>(T obj, string FilePath) { FileStream fs; byte[] buffer;
if (null == obj || null == FilePath || FilePath.Trim().Length <= 0) { return; }
if (!Directory.Exists(Path.GetDirectoryName(FilePath))) { Directory.CreateDirectory(Path.GetDirectoryName(FilePath)); }
fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.Write); buffer = new UTF8Encoding(false).GetBytes(Serialize<T>(obj)); fs.Write(buffer, 0, buffer.Length); fs.Close(); }
public static T ReadObjectFromFile<T>(string FilePath) { FileStream fs; byte[] buffer; string content;
if (null == FilePath || FilePath.Trim().Length <= 0 || !File.Exists(FilePath)) { return default(T); }
fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read); buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); content = GetUTF8String(buffer); fs.Close();
return Deserialize<T>(content); }
/// <summary> /// XML文件用記事本儲存一下就會導致檔案帶有BOM標記,在解析帶BOM標記的XML時會出錯。此方法自動識別是否帶有BOM標記,有則去掉BOM標記。 /// </summary> /// <param name="buffer"></param> /// <returns></returns> protected static string GetUTF8String(byte[] buffer) { if (buffer == null) return null;
if (buffer.Length <= 3) { return Encoding.UTF8.GetString(buffer); }
byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf }; //BOM標記
if (buffer[0] == bomBuffer[0] && buffer[1] == bomBuffer[1] && buffer[2] == bomBuffer[2]) { return new UTF8Encoding(false).GetString(buffer, 3, buffer.Length - 3); }
return Encoding.UTF8.GetString(buffer); } } }