C#物件序列化之坑
序列化的方法很簡單,如下:
/// <summary>
/// 文字化XML序列化
/// </summary>
/// <param name="Obj">物件</param>
public static string ToXml1<T>(T Obj)
{
XmlSerializerNamespaces theNames = new XmlSerializerNamespaces();
theNames.Add("", "");
XmlSerializer theSerializer = new XmlSerializer(Obj.GetType());
StringBuilder theSB = new StringBuilder();
var theXmlWS = new XmlWriterSettings();
theXmlWS.Encoding = Encoding.UTF8;
var theXml = "";
using (XmlWriter writer = XmlWriter.Create(theSB, theXmlWS))
{
theSerializer.Serialize(writer, Obj, theNames);
theXml = theSB.ToString();
}
return theXml;
}
注意,如果不加空的名稱空間,序列化的時候會加一些預設的名字空間,非常不好;但這個方法有個問題,就是輸出的是utf-16,中間那個設定為utf8沒鳥用,為了序列化成utf-8,如下寫:
/// <summary>
/// 文字化XML序列化
/// </summary>
/// <param name="Obj">物件</param>
public static string ToXml<T>(T Obj, Encoding TextEncoding = null)
{
if (TextEncoding == null)
{
TextEncoding = Encoding.UTF8;
}
XmlSerializerNamespaces theNames = new XmlSerializerNamespaces();
theNames.Add("", "");
MemoryStream theMS = new MemoryStream();
StreamWriter theTextWriter = new StreamWriter(theMS, TextEncoding);
XmlSerializer theSerializer = new XmlSerializer(Obj.GetType());
var theXml = "";
using (XmlWriter writer = XmlWriter.Create(theTextWriter))
{
theSerializer.Serialize(writer, Obj, theNames);
var theDataBytes = theMS.GetBuffer();
theXml = TextEncoding.GetString(theDataBytes);
}
theTextWriter.Close();
theMS.Close();
return theXml;
}
上面程式碼序列化正常,但IE瀏覽器可能對上述結果無法正常顯示,經過除錯我發現,序列化時在頭部會多一個空白字元,經過修改,如下才是正常:
/// <summary>
/// 文字化XML序列化
/// </summary>
/// <param name="Obj">物件</param>
public static string ToXml<T>(T Obj, Encoding TextEncoding = null)
{
if (TextEncoding == null)
{
TextEncoding = Encoding.UTF8;
}
XmlSerializerNamespaces theNames = new XmlSerializerNamespaces();
theNames.Add("", "");
MemoryStream theMS = new MemoryStream();
StreamWriter theTextWriter = new StreamWriter(theMS, TextEncoding);
XmlSerializer theSerializer = new XmlSerializer(Obj.GetType());
var theXml = "";
using (XmlWriter writer = XmlWriter.Create(theTextWriter))
{
theSerializer.Serialize(writer, Obj, theNames);
var theDataBytes = theMS.GetBuffer();
theXml = TextEncoding.GetString(theDataBytes);
}
theTextWriter.Close();
theMS.Close();
var thePos = theXml.IndexOf("<");
return theXml.Substring(thePos);
}
在跟對方做介面時發現,如果序列化和反序列化都是C#,問題不大,但如果對方靠解析,就會出問題,因為前面那個空白字元,估計很多程式都沒做自動濾掉處理。