读取XML文件

来源:互联网 发布:罗马 汉 知乎 编辑:程序博客网 时间:2024/06/11 06:32
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "Config/Page.xml";
            if (!File.Exists(path))
            {
                return;
            }


            //载入页面设置(读取XML文件)
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(path);


                if (xmlDoc == null)
                {
                    return;
                }


                XmlNodeList nodes = xmlDoc.SelectNodes("/PageCache/Page");
                string name = string.Empty;
                int time = 0;
                foreach (XmlNode node in nodes)
                {
                    name = "/" + node.Attributes["Name"].Value.ToString().TrimStart('/').ToLower();  //XML 文件节点名为Name的值,如果第一个位置没有“/”则添加,如果有,则忽略。
                    time = Shove._Convert.StrToInt(node.Attributes["CacheTime"].Value, 0); //取节点名为CacheTime的值


                    if (string.IsNullOrEmpty(name))
                    {
                        return;
                    }


                    _Settings.Add(name, time);
                }
            }
            catch (Exception ex)
            {
                SitePublicFunction.RecordError(ex);
            }
            finally
            {
                xmlDoc = null;
            }
0 0