C#讀取xml文件靜態類
阿新 • • 發佈:2018-01-14
col tex oca spa void n) bin log element
internal static class XmlUtil { internal static XmlDocument doc; internal static string path; //在以下情況下執行該函數:1).當class不為static,實例化class時;2).當調用靜態字段、方法、屬性等時 //但該函數僅執行一次 static XmlUtil() { path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); path= Path.Combine(path, "Config.xml"); doc = new XmlDocument(); doc.Load(path); } internal static void SetValue(string valueName, string value) { XmlNode node = doc.DocumentElement.SelectSingleNode(valueName); node.InnerText= value; doc.Save(path); } internal static string GetValue(string valueName) { XmlNode node = doc.DocumentElement.SelectSingleNode(valueName); string result = node.InnerText; return result; } }
用法:
staticvoid Main(string[] args) { string v1 = XmlUtil.GetValue(@"/config/A2/AA1"); Console.WriteLine(v1); v1 = XmlUtil.GetValue(@"A2/AA1"); Console.WriteLine(v1); Console.ReadLine(); }
C#讀取xml文件靜態類