简单方法hashtable持久化到xml文件中

【简单方法hashtable持久化到xml文件中】 .net framework中又可以持久化对象到xml中的类,但是我没有用过呀,也没有去看怎么用,于是自己写了个类:

using System.Collections; using System.Xml; namespace Test { public class KeyValueInfo { public Hashtable GetKeyValue(string path){if (System.IO.File.Exists(path)){ Hashtable kvs=new Hashtable(); XmlDocument xdoc = new XmlDocument(); xdoc.Load(path); XmlNodeList eles = xdoc.DocumentElement.ChildNodes; foreach (XmlElement xmlElement in eles){ kvs.Add(xmlElement["Key"].InnerText, xmlElement["Value"].InnerText); }return kvs; } return null; }public voidSaveKeyValue(Hashtable kvs,string path){ XmlDocument xdoc = new XmlDocument(); XmlDeclaration declaration = xdoc.CreateXmlDeclaration("1.0", "gb2312", "yes"); XmlElement sets = xdoc.CreateElement("Sets"); //根元素 foreach (DictionaryEntry kv in kvs){ XmlElement ele = xdoc.CreateElement("Element"); XmlElement key = xdoc.CreateElement("Key"); key.InnerText = kv.Key.ToString(); XmlElement value = https://www.it610.com/article/xdoc.CreateElement("Value"); value.InnerText = kv.Value.ToString(); ele.AppendChild(key); ele.AppendChild(value); sets.AppendChild(ele); } xdoc.AppendChild(declaration); xdoc.AppendChild(sets); xdoc.Save(path); } } }



    推荐阅读