xml文件操作
来源:互联网 发布:恐怖小说推荐知乎 编辑:程序博客网 时间:2024/04/30 16:03
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>