zoukankan      html  css  js  c++  java
  • Xml帮助类

       public class XMLHelper
        {
            #region  将xml文件转换为object对象类型 
            /// <summary>
            /// 将xml文件转换为object对象类型 
            /// </summary>
            /// <param name="path">xml文件的路径</param>
            /// <param name="type">要转换的类型的Type类型</param>
            /// <returns></returns>
            public static object ConvertXMLToObject(string path, Type type)
            {
                object obj = null;
                using (StreamReader reader = new StreamReader(path))
                {
                    string content = reader.ReadToEnd();
                    if (null == content)
                    {
                        throw new ArgumentNullException("xml");
                    }
                    if (null == type)
                    {
                        throw new ArgumentNullException("type");
                    }
                    XmlSerializer serializer = new XmlSerializer(type);
                    StringReader strReader = new StringReader(content);
                    XmlReader xreader = new XmlTextReader(strReader);
                    try
                    {
                        obj = serializer.Deserialize(xreader);
                    }
                    catch(InvalidOperationException ie)
                    {
                        throw new InvalidOperationException("Can not convert xml to object", ie);
                    }
                    finally
                    {
                        xreader.Close();
                    }
                    return obj;
                }
            }
            #endregion
    
            #region  将object对象转成xml保存
            /// <summary>
            /// 将object对象转成xml保存
            /// </summary>
            /// <param name="path">保存到xml文件的路径</param>
            /// <param name="obj">待转换的对象</param>
            /// <param name="toBeIndented">xml是否缩进 true:缩进,false:不缩进</param>
            public static void SaveObjectXML(string path, object obj, bool toBeIndented=true)
            {
                if (null == obj)
                {
                    throw new ArgumentNullException("obj");
                }
                UTF8Encoding encoding = new UTF8Encoding(false);
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                MemoryStream stream = new MemoryStream();
                XmlTextWriter writer = new XmlTextWriter(stream, encoding);
                writer.Formatting = toBeIndented ? Formatting.Indented : Formatting.None;
                try
                {
                    serializer.Serialize(writer, obj);
                }
                catch (InvalidOperationException ie)
                {
                    throw new InvalidOperationException("Can not convert object to xml.", ie);
                }
                finally
                {
                    writer.Close();
                }
                string xml = encoding.GetString(stream.ToArray());
                using (StreamWriter swriter = new StreamWriter(path))
                {
                    swriter.Write(xml);
                }
            } 
            #endregion
    
        }
    View Code
  • 相关阅读:
    IT职业选择与定位
    零碎时间应该拿来做什么
    编程漫谈(七):意义与自由
    第一次项目发布的心得体会
    入职一月的一点感想
    职业发展思考(一)
    健康先行: 每天锻炼一小时!!!
    2012, 软件职场之旅启程
    程序员的成长之路
    计算机学习方法
  • 原文地址:https://www.cnblogs.com/zhuyuchao/p/5891038.html
Copyright © 2011-2022 走看看