zoukankan      html  css  js  c++  java
  • 将对象序列化

    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;      
     
            /// <summary>
            
    /// 将对象转换为 XML 字符串        
            
    /// </summary>
            
    /// <param name="obj"></param>
            
    /// <returns></returns>
            public static string ToXmlString(Model.Student obj)
            {
                
    using (MemoryStream ms = new MemoryStream())
                {
                    System.Type type 
    = obj.GetType();
                    XmlSerializer serializer 
    = new XmlSerializer(type);
                    
    try
                    {
                        serializer.Serialize(ms, obj);
                        ms.Close();
                        
    return Encoding.UTF8.GetString(ms.ToArray());
                    }
                    
    catch (Exception ex)
                    {
                        Debug.Print(ex.ToString());
                        
    return string.Empty;
                    }
                }
            }


            
    /// <summary>
            
    /// 将对象转换为 XML 文件        
            
    /// </summary>
            
    /// <param name="obj"></param>
            
    /// <returns></returns>
            public static void ToXmlFile(Model.Student obj)
            {
                
    using (FileStream fs = new FileStream(@"D:\xsdj.xml", FileMode.Create))
                {
                    System.Type type 
    = obj.GetType();
                    XmlSerializer serializer 
    = new XmlSerializer(type);
                    
    try
                    {
                        serializer.Serialize(fs, obj);
                        fs.Close();
                    }
                    
    catch (Exception ex)
                    {
                        Debug.Print(ex.ToString());
                    }
                }
            }
  • 相关阅读:
    理解Python的With语句
    python按行读取文件,如何去掉换行符" "
    如何使用科大 mirrors 加速 pip?
    python os.path模块常用方法详解
    Python ConfigParser的使用
    mono-project
    Ubuntu 15.04下MySQL 5.6.25不支持中文解决办法
    ubuntu 15.04开放mysql远程连接
    在Linux(Ubuntu/openSUSE/CentOS)下配置ASP.NET(Apache + Mono)转载+补充
    Asp.net MVC @Html.DisplayNameFor中文乱码解决办法
  • 原文地址:https://www.cnblogs.com/panjun/p/2176931.html
Copyright © 2011-2022 走看看