參考引用:
--
1. 序列化(Serialize):將一個物件實體轉換成 XML 格式
public static string SpecificationDataTableSerialize(dsSpecifications.SpecificationDataTable dt)
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(dt.GetType());
StringBuilder sb = new StringBuilder();
System.IO.StringWriter writer = new System.IO.StringWriter(sb);
ser.Serialize(writer, dt);
return sb.ToString();
}
2. 反序列化(Deserialize):將一個 XML 文件轉成物件實體
public static dsSpecifications.SpecificationDataTable SpecificationDataTableDeserialize(string s)
{
dsSpecifications.SpecificationDataTable dt = new dsSpecifications.SpecificationDataTable();
// 如果傳入的 s 字串不是有效的 XML 格式的話,會發生 Exception,記得要做好 Error Handling
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.LoadXml(s);
System.Xml.XmlNodeReader reader = new System.Xml.XmlNodeReader(xdoc.DocumentElement);
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(dt.GetType());
object obj = ser.Deserialize(reader);
return obj as dsSpecifications.SpecificationDataTable;
}
沒有留言:
張貼留言