C# web.config文件配置属性映射到实体代码
public class TFSApiOptionsHelper
    {
        private static TFSApiOptionsHelper _onfigHelper = null;
        private static readonly object locker = new object();
        private static object lockHelper = new object();
        private static TFSApiOptions _options;
        static XmlDocument doc = new XmlDocument();
        //取配置地址 
        public static string ConfigBasePath = AppDomain.CurrentDomain.BaseDirectory;
        public static TFSApiOptionsHelper GetWebConfig
        {
            get
            {
                if (_onfigHelper == null)
                {
                    lock (locker)
                    {
                        string[] configFileName = new string[] { ConfigBasePath, "web.config" };
                        string filePath = Path.Combine(configFileName);
                        _onfigHelper = new TFSApiOptionsHelper();
                        doc.Load(filePath);
                    }
                }
                return _onfigHelper;
            }
        }
        /// <summary>
        /// 取配置文件
        /// </summary>
        /// <returns></returns>
        public static TFSApiOptions GetConfig()
        {
            if(_options !=null)
            {
                return _options;
            }
string[] configFileName = new string[] { ConfigBasePath, "config", "tfs.config" };
            string filePath = Path.Combine(configFileName);
            //如果配置 文件存在
            if (System.IO.File.Exists(filePath))
            {
                _options = Deserialize(filePath);
            }
            return _options;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configFilePath"></param>
        /// <returns></returns>
        public static TFSApiOptions Deserialize(string configFilePath)
        {
            lock (lockHelper)
            {
                return (TFSApiOptions)SerializationHelper.Load(typeof(TFSApiOptions), configFilePath);
            }
}
        /// <summary>
        /// 查找 是否有 配置 WeChatPayNotify 节点
        /// </summary>
        public bool IsTFSApiPayNotify
        {
            get
            {
                bool flag = false;
                XmlNode node = doc.SelectSingleNode("configuration/system.webServer/ handlers");
//doc.SelectSingleNode("configuration/system.webServer/ handlers").ChildNodes[0].Attributes["name"]
                if (node == null || node.ChildNodes.Count == 0)
                {
                    return flag;
                }
                foreach (XmlNode item in node.ChildNodes)
                {
                    if (item.Attributes != null && item.Attributes["name"] != null)
                    {
                        string name = item.Attributes["name"].Value;
                        //找到 配置节点 名称 返回
                        if (name == "PayNotify")
                        {
                            flag = true;
                            return flag;
                        }
                    }
}
                return flag;
            }
        }
}
/// <summary>
    /// SerializationHelper 的摘要说明。
    /// </summary>
    public class SerializationHelper
    {
        private SerializationHelper()
        { }
private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>();
        public static XmlSerializer GetSerializer(Type t)
        {
            int type_hash = t.GetHashCode();
            if (!serializer_dict.ContainsKey(type_hash))
                serializer_dict.Add(type_hash, new XmlSerializer(t));
            return serializer_dict[type_hash];
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="filename">文件路径</param>
        /// <returns></returns>
        public static object Load(Type type, string filename)
        {
            FileStream fs = null;
            try
            {
                // open the stream...
                fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(type);
                return serializer.Deserialize(fs);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="filename">文件路径</param>
        public static bool Save(object obj, string filename)
        {
            bool success = false;
            FileStream fs = null;
            // serialize it...
            try
            {
                fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(fs, obj);
                success = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return success;
}
        /// <summary>
        /// xml序列化成字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>xml字符串</returns>
        public static string Serialize(object obj)
        {
            string returnStr = "";
            XmlSerializer serializer = GetSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            XmlTextWriter xtw = null;
            StreamReader sr = null;
            try
            {
                xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
                xtw.Formatting = System.Xml.Formatting.Indented;
                serializer.Serialize(xtw, obj);
                ms.Seek(0, SeekOrigin.Begin);
                sr = new StreamReader(ms);
                returnStr = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (xtw != null)
                    xtw.Close();
                if (sr != null)
                    sr.Close();
                ms.Close();
            }
            return returnStr;
}
        public static object DeSerialize(Type type, string s)
        {
            byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
            try
            {
                XmlSerializer serializer = GetSerializer(type);
                return serializer.Deserialize(new MemoryStream(b));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
                    
                
                
            
        
浙公网安备 33010602011771号