zoukankan      html  css  js  c++  java
  • xml格式文件读取(读取网站自定义配置文件)

    private static object obj = new object();
            private NameValueCollection _system = null;
            /// <summary>
            /// 得到系统配置表信息
            /// </summary>
            /// <returns>NameValueCollection 对象</returns>
            public  NameValueCollection GetSystem()
            {
                lock (obj)
                {
                    if (_system == null)
                    {
                        if (HttpContext.Current == null || HttpContext.Current.Cache == null || HttpContext.Current.Cache["system"] == null)
                        {
                            //read xml
                            string path = AppDomain.CurrentDomain.BaseDirectory + "config\\data.config";
                            initSystemConfig();
                            NameValueCollection nvc = new NameValueCollection();
                            XmlDocument Doc_Detail = new XmlDocument();
                            Doc_Detail.Load((path));
                            XmlNodeList NodeList = Doc_Detail.SelectNodes("/root/system/*");
                            if (NodeList.Count > 0)
                            {
                                for (int i = 0; i < NodeList.Count; i++)
                                {
    
                                    if (NodeList[i] != null)
                                    {
                                        nvc.Add(NodeList[i].Name, NodeList[i].InnerText);
                                    }
    
                                }
                            }
                            _system = nvc;
                            if (HttpContext.Current != null)
                                HttpContext.Current.Cache.Add("system", nvc, new System.Web.Caching.CacheDependency(HttpContext.Current.Server.MapPath("/config/data.config")), System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
                        }
                        else
                        {
                            _system = (NameValueCollection)HttpContext.Current.Cache["system"];
    
                        }
                    }
                }
                return _system;
            }
     /// <summary>
            /// 初始化文件信息
            /// </summary>
            private void initSystemConfig()
            {
                string path1 = AppDomain.CurrentDomain.BaseDirectory+"config";
                if (!Directory.Exists(path1))
                {
                    Directory.CreateDirectory(path1);
    
                }
                string path = AppDomain.CurrentDomain.BaseDirectory+"config\\data.config";
                FileInfo CreateFile = new FileInfo(path); //创建文件
                if (!CreateFile.Exists)
                {
                    FileStream FS = CreateFile.Create();
                    FS.Close();
    
                    StreamWriter SW;
                    SW = File.AppendText(path);
                    SW.WriteLine("<?xml version=\"1.0\"?><root><system></system></root>");
                    SW.Close(); 
                }
            }

    //知识点:
    //NameValueCollection 作为键值对存储数据
    //
    xmlDoc.SelectNodes("/root/system/*"); 以反斜杠"/"分开得到可以 XmlNodeList 列表。


  • 相关阅读:
    2021/1/10周总结一
    java基础复习五
    java基础复习四
    构建之法读书笔记三
    javaweb学生教务系统
    java基础复习三
    关于eclipse项目错误的一些小经验总结
    Java基础复习二
    JavaDoc文档
    2017-2018-2 20179216 《网络攻防与实践》 第四周总结
  • 原文地址:https://www.cnblogs.com/lztkdr/p/lzmj.html
Copyright © 2011-2022 走看看