专注于.Net

享受编程的乐趣
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

序列化与反序列化

Posted on 2011-11-22 17:06  古道飘零客  阅读(146)  评论(0编辑  收藏  举报
/// 序列化DataTable
      
///</summary>
      
///<param name="pDt">包含数据的DataTable</param>
      
///<returns>序列化的DataTable</returns>
      publicstaticstring SerializeDataTableXml(DataTable pDt,string pdtname) {
          
// 序列化DataTable
           pDt.TableName = pdtname;
           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>
         publicstatic 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;
         }