//----------------------------------------------------------------------- // // Copyright (c) V1.0 // 作者:季健国 // 功能:加载XML // 历史版本:2013-11-26 新增 // //----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; using System.Web; namespace Ant.Common { public class LoadXmlHelper { /// /// 获取数据 /// public static XmlDocument XmlLoading(string configpath) { XmlDocument xmlDoc = new XmlDocument(); try { if (!string.IsNullOrEmpty(configpath)) { xmlDoc.Load(configpath); } } catch (Exception ex) { // Log4NetHelper.WriteExceptionLog("加载URL文件失败", ex); } return xmlDoc; } /// /// 查找XML所在节点的值 /// 添加人:季健国 /// 添加时间:2013-12-03 /// /// /// /// public static object GetXmlValue(XmlDocument xml,string index) { object obj = null; if (!Equals(xml.SelectSingleNode(index).InnerText,null)) obj = xml.SelectSingleNode(index).InnerText; return obj; } } /// /// 数据库连接配置缓存文件对象 /// public class DataBaseXmlObject { /// /// 数据库连接配置缓存文件对象 /// public DataBaseXmlObject() { } /// /// 数据库连接配置缓存文件对象 /// /// 数据库类型 /// 地址 /// 验证方式 /// 账号 /// 密码 public DataBaseXmlObject(int dataBaseType, string dataBaseAddress, int authentication, string account, string password) { this.DataBaseAccount = account; this.DataBasePassword = password; this.DataBaseType = dataBaseType; this.DataBaseAddress = dataBaseAddress; this.DataBaseAuthentication = authentication; this.LoginTime = DateTime.Now; } /// /// 数据库类型 /// public int DataBaseType { get; set; } /// /// 数据库地址 /// public string DataBaseAddress { get; set; } /// /// 验证方式 /// public int DataBaseAuthentication { get; set; } /// /// 账号 /// public string DataBaseAccount { get; set; } /// /// 密码 /// public string DataBasePassword { get; set; } /// /// 登陆时间 /// public DateTime LoginTime { get; set; } /// /// 获取配置文件信息 /// /// 路径 /// 返回集合 public static Dictionary GetList(string path) { if (File.Exists(path)) { XmlDocument doc = new XmlDocument(); doc.LoadXml(ReaderFile(path).Trim()); Dictionary list = new Dictionary(); foreach (XmlNode databaseConfig in doc.DocumentElement.ChildNodes) { string address = StringEncrypt.DeCode(databaseConfig.Attributes["address"].Value); DataBaseXmlObject dataBase = new DataBaseXmlObject(); dataBase.DataBaseAccount = StringEncrypt.DeCode(databaseConfig.Attributes["account"].Value); dataBase.DataBasePassword = StringEncrypt.DeCode(databaseConfig.Attributes["password"].Value); dataBase.DataBaseAuthentication = int.Parse(StringEncrypt.DeCode(databaseConfig.Attributes["authentication"].Value)); dataBase.DataBaseType = int.Parse(StringEncrypt.DeCode(databaseConfig.Attributes["dataBaseType"].Value)); dataBase.DataBaseAddress = address; dataBase.LoginTime = DateTime.Parse(StringEncrypt.DeCode(databaseConfig.Attributes["login"].Value)); if (list.ContainsKey(address) == false) { list.Add(address, dataBase); } } return list; } return null; } /// /// 获取连接字符串 /// /// 数据库 public string GetConnectionString(string dataBase) { if (DataBaseAuthentication == 0) { return "Data Source=" + DataBaseAddress + ";Initial Catalog=" + dataBase + ";Integrated Security=True;"; } return "user id=" + DataBaseAccount + ";password=" + DataBasePassword + ";DataBase=" + dataBase + ";server=" + DataBaseAddress + ";"; } /// /// 读文件 /// /// 路径 /// 返回内容 public static string ReaderFile(string path) { if (File.Exists(path)) { FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader reader = new StreamReader(f, Encoding.UTF8); string content = reader.ReadToEnd(); reader.Close(); f.Close(); return content; } return string.Empty; } } }