class clsXML { /*eig: xml内容: <?xml version="1.0" encoding="utf-8" ?> <setting id="parent"> <SP_Master id="SP_Master" tableName ="oders_Master " /> </setting> 调用方法: string tableName = clsXML.getvalue("SP_Master", "tableName"); */ public static string getvalue(string _xmlPath, string _id, string _propertyName) { string rntValue = string.Empty; //这一部分路径处理的 ,可以不用管 if (Path.IsPathRooted(_xmlPath) == false) { if (_xmlPath.Trim().Substring(0, 1) == ".") _xmlPath = AppDomain.CurrentDomain.BaseDirectory + _xmlPath.Trim().Substring(1); else _xmlPath = AppDomain.CurrentDomain.BaseDirectory + _xmlPath.Trim(); } if (!File.Exists(_xmlPath)) { return string.Empty; } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(_xmlPath); XmlElement xmlnode = (XmlElement)xmlDoc.SelectSingleNode("//*[@id='" + _id.ToString().Trim() + "']"); if (xmlnode != null) { rntValue = xmlnode.Attributes[_propertyName].Value; if (rntValue.Trim() == "") rntValue = string.Empty; } return rntValue; } public static string getvalue(string _id, string _propertyName) { //通过工程属性设置的xml配置文件来得到相关配置项(windowsServicePart.Properties.Settings.Default.configFilePath) return clsXML.getvalue(windowsServicePart.Properties.Settings.Default.configFilePath, _id, _propertyName); } /*eig: xml内容: <?xml version="1.0" encoding="utf-8" ?> <setting id="parent"> <connectstring id="connectstring">Data Source=127.0.0.1 ;Initial Catalog=myDB;User ID=sa; Password=sa</connectstring> </setting> 调用方法: string connectString = clsXML.getvalue("connectstring") */ public static string getvalue(string _id) { string rntValue = string.Empty; string _xmlPath = windowsServicePart.Properties.Settings.Default.configFilePath; if (Path.IsPathRooted(_xmlPath) == false) { if (_xmlPath.Trim().Substring(0, 1) == ".") _xmlPath = AppDomain.CurrentDomain.BaseDirectory + _xmlPath.Trim().Substring(1); else _xmlPath = AppDomain.CurrentDomain.BaseDirectory + _xmlPath.Trim(); } if (!File.Exists(_xmlPath)) { return string.Empty; } try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(_xmlPath); XmlElement xmlnode = (XmlElement)xmlDoc.SelectSingleNode("//*[@id='" + _id.ToString().Trim() + "']"); if (xmlnode != null) { rntValue = xmlnode.InnerText; if (rntValue.Trim() == "") rntValue = string.Empty; } } catch { return string.Empty; } return rntValue; } }