xml文件操作

C#:
using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Xml;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.Data;

using System.Data.Sql;



namespace DataProcessing

{

    class Config

    {

        private static string path = Environment.CurrentDirectory + "//config.xml";



        #region 将配置信息写入配置文件

        public static bool writeConfig(string Server,string User,string Password)

        {

            try

            {

                FileInfo fileInfo = new FileInfo(path);

                if (!fileInfo.Exists)

                {

                    createConfigFile();

                }

                XmlDocument xmlDocument = new XmlDocument();

                xmlDocument.Load(fileInfo.FullName);



                foreach (XmlNode node in xmlDocument["configuration"]["appSettings"].ChildNodes)

                {

                    if (node.Name == "add")

                    {

                        if (node.Attributes["key"].Value == "Server")

                        {

                            node.Attributes["value"].Value =Server;

                        }

                        else if (node.Attributes["key"].Value == "User id")

                        {

                            node.Attributes["value"].Value =User;

                        }

                        else if (node.Attributes["key"].Value == "Password")

                        {

                            node.Attributes["value"].Value =Password;

                        }

                    }

                }

                xmlDocument.Save(fileInfo.FullName);

                return true;

            }

            catch (Exception er)

            {

                MessageBox.Show(er.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return false;

            }

        }

        #endregion



        #region 读取配置文件(根据key返回相应的value

        public static string readConfig(string key)

        {

            try

            {

                string value = "";

                FileInfo fileInfo = new FileInfo(path);

                if (!fileInfo.Exists)

                {

                    createConfigFile();

                }

                XmlDocument xmlDocument = new XmlDocument();

                xmlDocument.Load(fileInfo.FullName);



                foreach (XmlNode node in xmlDocument["configuration"]["appSettings"].ChildNodes)

                {

                    if (node.Name == "add")

                    {

                        if (node.Attributes["key"].Value == key)

                        {

                            value = node.Attributes["value"].Value;

                        }

                    }

                }

                return value;

            }

            catch (Exception er)

            {

                MessageBox.Show(er.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return null;

            }

        }

        #endregion



        #region 设置登录信息

        public static bool setLoginInfo(string LoginUser,string LoginPassword)

        {

            try

            {

                FileInfo fileInfo = new FileInfo(path);

                if (!fileInfo.Exists)

                {

                    createConfigFile();

                }

                XmlDocument xmlDocument = new XmlDocument();

                xmlDocument.Load(fileInfo.FullName);



                foreach (XmlNode node in xmlDocument["configuration"]["appSettings"].ChildNodes)

                {

                    if (node.Name == "add")

                    {

                        if (node.Attributes["key"].Value == "LoginUser")

                        {

                            node.Attributes["value"].Value = LoginUser;

                        }

                        else if (node.Attributes["key"].Value == "LoginPassword")

                        {

                            node.Attributes["value"].Value = Encryption.MD5(LoginPassword);

                        }

                    }

                }

                xmlDocument.Save(fileInfo.FullName);

                return true;

            }

            catch (Exception er)

            {

                MessageBox.Show(er.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return false;

            }

        }

        #endregion



        #region 获得连接字符串

        public static string getConnectionString(string DBName)

        {

            StringBuilder constr = new StringBuilder();

            constr.Append("server=");

            constr.Append(readConfig("Server"));

            if (DBName != string.Empty)

            {

                constr.Append(";database=");

                constr.Append(DBName);

            }

            constr.Append(";user id=");

            constr.Append(readConfig("User id"));

            constr.Append(";password=");

            constr.Append(readConfig("Password"));

            return constr.ToString();

        }

        #endregion



        #region 创建配置文件

        public static void createConfigFile()

        {

            try

            {

                FileInfo fileInfo = new FileInfo(path);

                if (!fileInfo.Exists)

                {

                    XmlTextWriter writer = new XmlTextWriter(path,Encoding.UTF8);

                    writer.Formatting = Formatting.Indented;

                    writer.WriteStartDocument();

                    writer.WriteStartElement("configuration");

                    writer.WriteStartElement("appSettings");



                    writer.WriteStartElement("add");

                    writer.WriteAttributeString("key", "Server");

                    writer.WriteAttributeString("value", "127.0.0.1");

                    writer.WriteEndElement();



                    writer.WriteStartElement("add");

                    writer.WriteAttributeString("key", "User id");

                    writer.WriteAttributeString("value", "sa");

                    writer.WriteEndElement();



                    writer.WriteStartElement("add");

                    writer.WriteAttributeString("key", "Password");

                    writer.WriteAttributeString("value", "");

                    writer.WriteEndElement();



                    writer.WriteStartElement("add");

                    writer.WriteAttributeString("key", "LoginUser");

                    writer.WriteAttributeString("value", "sa");

                    writer.WriteEndElement();



                    writer.WriteStartElement("add");

                    writer.WriteAttributeString("key", "LoginPassword");

                    writer.WriteAttributeString("value", "c12e01f2a13ff5587e1e9e4aedb8242d");

                    writer.WriteEndElement();



                    writer.WriteEndElement();

                    writer.WriteEndElement();

                    writer.WriteEndDocument();

                    writer.Flush();

                    writer.Close();

                }

            }

            catch (Exception er)

            {

                throw new Exception(er.Message);

            }

        }

        #endregion

    }

}



config.xml文件:
<?xml version="1.0" encoding="utf-8"?>

<configuration>

  <appSettings>

    <add key="Server" value="serverurl" />

    <add key="User id" value="dbuser" />

    <add key="Password" value="password" />

    <add key="LoginUser" value="username" />

    <add key="LoginPassword" value="52ba8e68faf75a9771420d45fda64955" />

  </appSettings>

</configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值