xml配置文件推荐方式

1.XML帮助类

    /// <summary>
    /// Xml帮助类
    /// </summary>
    public  class XmlHelper
    {
        /// <summary>
        /// 保存xml
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="t"></param>
        public static void SaveXml<T>(string path, T t)
        {
            StreamWriter writer = new StreamWriter(path);

            new XmlSerializer(typeof(T)).Serialize(writer, t);

            writer.Close();
        }

        /// <summary>
        ///  将文件转换成实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="file"></param>
        /// <returns></returns>
        public static T LoadXml<T>(string file)
        {
            StreamReader reader = new StreamReader(file);

            object obj = new XmlSerializer(typeof(T)).Deserialize(reader);

            reader.Close();

            return (T)obj;
        }
        /// <summary>
        /// 将XML转换成实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T FromXml<T>(string xml)
        {
            StringReader reader = new StringReader(xml);

            object obj = new XmlSerializer(typeof(T)).Deserialize(reader);

            reader.Close();

            return (T)obj;
        }
        /// <summary>
        /// 将实体对象转换为xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ToXml<T>(T t)
        {
            StringBuilder builder = new StringBuilder();
            StringWriter writer = new StringWriter(builder);

            new XmlSerializer(typeof(T)).Serialize(writer, t);

            writer.Close();

            return builder.ToString();
        }

        /// <summary>
        /// 将xml转换为实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlPath">xml路径</param>
        /// <returns></returns>
        public static T LoadXmlFile<T>(string xmlPath) where T : class
        {
            if (!File.Exists(xmlPath))
            {
                return null;
            }
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            StreamReader streamReader = null;

            try
            {
                streamReader = new StreamReader(xmlPath);
                return (T)(xmlSerializer.Deserialize(streamReader));
            }
            catch
            {
                throw;
            }
            finally
            {
                if (null != streamReader)
                {
                    streamReader.Close();
                }
            }
        }
    }

2.配置范例

    [XmlRoot("globalConfig")]
    public class GlobalConfiguration
    {
        [XmlElement("UnExportIndexCode")]
        public string UnExportIndexCode
        {
            get;
            set;
        }

        [XmlElement("FuturesRebarIndexCode")]
        public string FuturesRebarIndexCode
        {
            get;
            set;
        }
 
      
    }

3.配置范例

 

4.全局调用类

   public class RuntimeConfiguration
    {
        private static readonly object SingleLock = new object();
        private static GlobalConfiguration instance_global;

        public static GlobalConfiguration Global
        {
            get
            {
                if (instance_global == null)
                {
                    lock (SingleLock)
                    {
                        if (instance_global == null)
                        {
                            instance_global = MySteel.Common.Helper.XmlHelper.LoadXml<GlobalConfiguration>(AppDomain.CurrentDomain.BaseDirectory + AppSettingsHelper.GetStringValue("GlobalConfig"));
                        }
                    }
                }
                return instance_global;
            }
        }
    }

 

posted @ 2017-02-20 09:32  About.Me(Name=jack)  阅读(174)  评论(0编辑  收藏  举报