做序列化是为了把一个数据结构转化成为String,进而转化为二进制byte,存入文件中。
C#的对象序列化记忆中有3种,Binary,XML和SOAP。SOAP不知道是啥~,据说是符合SOAP的xml序列化。
以下转载自:http://www.cnblogs.com/moonspell/archive/2007/07/17/821201.html
/// <summary> /// 序列化DataTable /// </summary> /// <param name="pDt">包含数据的DataTable</param> /// <returns>序列化的DataTable</returns> private static string SerializeDataTableXml(DataTable pDt) { StringBuilder sb = new StringBuilder(); XmlWriter writer = XmlWriter.Create(sb); XmlSerializer serializer = new XmlSerializer(typeof(DataTable)); serializer.Serialize(writer,pDt); writer.Close(); return sb.ToString(); }
/// <summary> /// 反序列化DataTable /// </summary> /// <param name="pXml">序列化的DataTable</param> /// <returns>DataTable</returns> public static DataTable DeserializeDataTable(string pXml) { StringReader strReader = new StringReader(pXml); XmlReader xmlReader = XmlReader.Create(strReader); XmlSerializer serializer = new XmlSerializer(typeof(DataTable)); DataTable dt = serializer.Deserialize(xmlReader) as DataTable; return dt; }原文链接:https://www.f2er.com/xml/300062.html