• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
FtpCsharp
博客园    首页    新随笔    联系   管理    订阅  订阅

Convert Between Class And Xml

 public  T DeSerializer<T>(string serializer)  

     {        

     DataContractSerializer ser = new DataContractSerializer(typeof(T));      

     MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(serializer));        

     T DatatractDto = (T)ser.ReadObject(ms);        

     ms.Close();      

     return DatatractDto;  

     }

public string Serialize<T>(T instance)  

     {          

       DataContractSerializer ser = new DataContractSerializer(instance.GetType());      

       using (MemoryStream ms = new MemoryStream())    

       {
                ser.WriteObject(ms, instance);
                StringBuilder sb = new StringBuilder();
                sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
                return sb.ToString();
        }  

     }

Description:

          DataContractSerializer Class   from      namespace of      System.Runtime.Serialization

         MemoryStream   Class  from namespace of System.IO

Import:

          how to  DeSerializer ? the xml string contains the namespace of T.It could based on the namespace to find T,then do the   DeSerializer .So,if we change the namespace of T,it will not success because of no finding T by namespace!        

posted @ 2011-04-08 18:32  FtpCsharp  阅读(202)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3