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());
                    }
                }
            }
  • 相关阅读:
    【Unity3D】3D游戏学习
    风投小观之敢于冒高风险,方能收高回报
    同步请求和异步请求的区别
    IOS开发UI基础学习-------总结
    我的哲学观-1000字例文
    Uva11292--------------(The Dragon of Loowater)勇者斗恶龙 (排序后贪心)
    学习笔记之vector向量容器
    欧几里得算法求最大公约数+最小公倍数
    二叉树的遍历规则(前序遍历、后序遍历、中序遍历)
    《winform窗体应用程序》----------简易记事本
  • 原文地址:https://www.cnblogs.com/panjun/p/2176931.html
Copyright © 2011-2022 走看看