C# 读写XML文件,用于配置文件

 

        public static void UpdateSetValue(string tagName, string value)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("config.xml"); //加载xml文件

           // XmlNode node = xmlDoc.GetElementById("Config");
            XmlNodeList nodes = xmlDoc.SelectSingleNode("Config").ChildNodes; 
            
            for (int i = 0; i < nodes.Count; i++)
            {
                MessageBox.Show(nodes[i].Name);
                if (nodes[i].Name.Equals(tagName))
                {
                    nodes[i].InnerText = value;
                } 
            }
            try
            {
                //保存上面的修改    
                xmlDoc.Save("config.xml");

            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static string GetNodeValue(string tagName)
        {
            string result = "";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("config.xml"); //加载xml文件

            // XmlNode node = xmlDoc.GetElementById("Config");
            XmlNodeList nodes = xmlDoc.SelectSingleNode("Config").ChildNodes;

            for (int i = 0; i < nodes.Count; i++)
            { 
                if (nodes[i].Name.Equals(tagName))
                {
                    result = nodes[i].innerText;
                   return result ;                   
                } 
            }
            return result;
        } 





  
    Eva
    56.5
  
  True
  True

你可能感兴趣的:(C#)