zoukankan      html  css  js  c++  java
  • C#Object与XML文件或二进制文件之间的转化

    Object To Xml 文件

     public static bool Serializer<T>(object obj, string path)
          {
              FileStream xmlfile = new FileStream(path, FileMode.OpenOrCreate);
              
              //创建序列化对象 
              XmlSerializer xml = new XmlSerializer(typeof(T));
              try
              {    //序列化对象
                  xml.Serialize(xmlfile, obj);
                  xmlfile.Close();
              }
              catch (InvalidOperationException)
              {
                  throw;
              }
              
              return true;
              
          }

    Xml To Object

     public static T Deserializer<T>(string path)
          {
              try
              {
                  FileStream xmlfile = new FileStream(path, FileMode.Open);
    
                  XmlSerializer xml = new XmlSerializer(typeof(T));
                  //序列化对象
                  //xmlfile.Close();
                  T t = (T)xml.Deserialize(xmlfile);
                  xmlfile.Close();
                  return t;
              }
              catch (InvalidOperationException)
              {
                  throw;
              }
              catch (FileNotFoundException)
              { throw; }
              finally
              {
                  
              }
          }
    

      

    Object To Bin

    public static bool BinarySerializer(object obj, string path)
          {
              FileStream Stream = new FileStream(path, FileMode.OpenOrCreate);
              //创建序列化对象 
              BinaryFormatter bin = new BinaryFormatter();
              try
              {    //序列化对象
                  bin.Serialize(Stream, obj);
                  Stream.Close();
              }
              catch (InvalidOperationException)
              {
                  throw;
              }
              return true;
          }
    

      

    Bin To Object

    public static T BinaryDeserializer<T>(string path)
          {
              try
              {
                  FileStream binfile = new FileStream(path, FileMode.Open);
    
                  BinaryFormatter bin = new BinaryFormatter();
                  //序列化对象
                  //xmlfile.Close();
                  T t = (T)bin.Deserialize(binfile);
                  binfile.Close();
                  return t;
              }
              catch (InvalidOperationException)
              {
                  throw;
              }
              catch (FileNotFoundException)
              { throw; }
              finally
              {
    
              }
          }
    

      

  • 相关阅读:
    sass08 if while for each
    sass07 函数
    sass06 mixin
    sass05 数据类型,数据运算
    sass04 嵌套、继承、占位符
    批量导出docker images 的一个简单方法
    ARM 版本 瀚高 数据库的启动命令
    PHPStorm+Wamp+Xdebug+Windows7调试代码
    在Windows Server 2012 中安装 .NET 3.5 Framework
    Windows Server 2012 GUI与Core的切换
  • 原文地址:https://www.cnblogs.com/jesszhu/p/3276556.html
Copyright © 2011-2022 走看看