C#:XML操作(簡單)

weixin_34262482發表於2014-04-18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

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

namespace MyVertion
{
    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 volatile 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()
        {
            return File.Exists(m_configPath);
        }

        //儲存、更改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;
            }
            else
            {
                xmlDoc.Load(m_configPath);
                XmlNode root = xmlDoc.SelectSingleNode("Connection");
                XmlNode xnd = root.SelectSingleNode(item);
                return xnd.InnerText;
            }
           
        }

    }
}

 

 

更多:https://i.cnblogs.com/EditPosts.aspx?postid=3673943

相關文章