C# Winform 读取XML以及修改

阅读更多

在调用方法之前,要右击选中的xml文件,设置xml 的 Coyp to output direcotory :Copy always

 XML:




  blue

winform方法:

 ///


        /// 读取默认皮肤
        ///

        ///
        public string ReadDefaultSkin()
        {
            //Server.MapPath(@"Projects.xml");

            XmlReader reader = new XmlTextReader(Application.StartupPath + @"\Xml\SkinConfig.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            XmlNode root = doc.DocumentElement;
            //选取DefaultLangugae节点
            XmlNode node = root.SelectSingleNode("//DefaultSkin");
            string result = "blue";
            if (node != null)
            {
                //取出节点中的内容
                result = node.InnerText;
            }
            reader.Close();
            return result;
        }


        ///


        /// 更新皮肤
        ///

        ///
        ///
        public void UpdateSkin(string skin)
        {
            string FileName = Application.StartupPath + @"\Xml\SkinConfig.xml";
            //初始化XML文档操作类
            XmlDocument myDoc = new XmlDocument();
            //加载XML文件
            myDoc.Load(FileName);

            //搜索指定的节点
            System.Xml.XmlNodeList nodes = myDoc.SelectNodes("//Skin");

            if (nodes != null)
            {
                foreach (System.Xml.XmlNode xn in nodes)
                {
                    xn.SelectSingleNode("DefaultSkin").InnerText = skin;
                }
            }

            myDoc.Save(FileName);

        }

 

你可能感兴趣的:(WinForm,XML,C,C++,C#)