C# XMLOperate

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using System.IO;
using System.Windows.Forms;

namespace ResultsManage
{
    class XMLOperate
    {
        private string m_configPath = Application.StartupPath + @"DatabaseConfig.xml";
        private string vertion = "1.0";
        private string encoding = "UTF-8";
        private string standalone = "no";
        private string comment = "database config";

        private XmlDocument xmlDoc = null;

        private static XMLOperate XmlOp = null;
        public static XMLOperate GetInstance()
        {
            if (null == XmlOp)
            {
                XmlOp = new XMLOperate(null);
            }
            return XmlOp;
        }

        public string _Vertion
        {
            set
            {
                vertion = value;
            }

            get
            {
                return vertion;
            }
        }

        public string _Encoding
        {
            set
            {
                encoding = value;
            }

            get
            {
                return encoding;
            }
        }

        public string _Standalone
        {
            set
            {
                standalone = value;
            }

            get
            {
                return standalone;
            }
        }

        public string _Comment
        {
            set
            {
                comment = value;
            }

            get
            {
                return comment;
            }
        }

        public string _ConfigPath
        {
            get
            {
                return m_configPath;
            }
        }
        
        #region 构造函数
        public XMLOperate(string xmlPath)
        {
            if (!string.IsNullOrEmpty(xmlPath))
            {
                m_configPath = xmlPath;
            }
            xmlDoc = new XmlDocument();
        }
        #endregion


        //创建configxml文件
        public void CreateConfigXml()
        {
            xmlDoc.CreateXmlDeclaration(vertion, encoding, standalone);
            xmlDoc.CreateComment(comment);

            XmlElement rootEle = xmlDoc.CreateElement("Connection");
            xmlDoc.AppendChild(rootEle);

            XmlElement ele = xmlDoc.CreateElement("Server");
            rootEle.AppendChild(ele);
            ele = xmlDoc.CreateElement("Instance");
            rootEle.AppendChild(ele);
            ele = xmlDoc.CreateElement("Database");
            rootEle.AppendChild(ele);
            ele = xmlDoc.CreateElement("UserName");
            rootEle.AppendChild(ele);
            //ele = xmlDoc.CreateElement("Password");
            //rootEle.AppendChild(ele);
            ele = xmlDoc.CreateElement("Vertion");
            rootEle.AppendChild(ele);

            xmlDoc.Save(m_configPath);

            //加入XML的声明段落,<?xml version="1.0" encoding="utf-8"?>
//            xmlDoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
//                <Connection>
//                </Connection>");
//            XmlNode root = xmlDoc.SelectSingleNode("Connection");
        }

        //检查配置文件是否存在
        public bool IsExist()
        {
            if(!File.Exists(m_configPath))
            {
                return false;
            }
            return true;
        }

        //保存、更改xml文件
        public bool UpdateConfigInfo(string server, string instance, string database, string username, string password, string version)
        {
            if (!IsExist())
            {
               return false;
            }
            XmlNode root = xmlDoc.SelectSingleNode("Connection");
            XmlNode xnd = root.SelectSingleNode("Server");
            xnd.InnerText = server;
            xnd = root.SelectSingleNode("Instance");
            xnd.InnerText = instance;
            xnd = root.SelectSingleNode("Database");
            xnd.InnerText = database;
            xnd = root.SelectSingleNode("UserName");
            xnd.InnerText = username;
            //xnd = root.SelectSingleNode("Password");
            //xnd.InnerText = password;
            xnd = root.SelectSingleNode("Vertion");
            if (!string.IsNullOrEmpty(version))
            {
                xnd.InnerText = version;
            }
            
            xmlDoc.Save(m_configPath);
            return true;
        }

        /// <summary>
        /// 读配置文件
        /// </summary>
        /// <returns></returns>
        public string ReadConfigInfo(string item)
        {
            if (!IsExist())
            {
                MessageBox.Show("配置文件不存在!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return null;
            }

            xmlDoc.Load(m_configPath);
            XmlNode root = xmlDoc.SelectSingleNode("Connection");
            XmlNode xnd = root.SelectSingleNode(item);
            return xnd.InnerText;
        }

    }
}
原文地址:https://www.cnblogs.com/shenchao/p/3556216.html