• C#:XML操作(简单)


    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

  • 相关阅读:
    重写/覆盖基类的事件
    关于“日志”的输出控制
    WPF中DataGrid垂直滚动条滚动后导致每行CheckBox选择错乱
    WPF窗体应用程序开发
    C# 操作自定义config文件
    WPF绑定数据源之RelativeSource
    C# http请求 设置代理(标题可以作为搜索关键字)
    前端加载特效
    实现不同的项目,用不同的git 账号提交
    Casbin 使用记录
  • 原文地址:https://www.cnblogs.com/shenchao/p/3673944.html
Copyright © 2020-2023  润新知