首页 > 代码库 > Asp.Net 读取xml文件中Key的值,并且过滤掉注释内容代码

Asp.Net 读取xml文件中Key的值,并且过滤掉注释内容代码

      /// <summary>
        /// 读取配置文件keys
        /// </summary>
        /// <returns></returns>
        public string _GetKeys()
        {
            string filename = Server.MapPath("/") + @"web.config";
            XmlDocument xmldoc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            settings.IgnoreWhitespace = true;
            XmlReader reader = XmlReader.Create(filename, settings);
            xmldoc.Load(reader);
            XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
            foreach (XmlElement element in topM)
            {
                if (element.Name == "appSettings")
                {
                    XmlNodeList node = element.ChildNodes;
                    if (node.Count > 0)
                    {
                        foreach (XmlElement el in node)
                        {
                            string str = el.Attributes["key"].Value.ToString();
                            AppConfigKeys += str.ToString() + ",";
                        }
                    }
                }
            }
            return AppConfigKeys;
        }