简单XML操作类
发布时间:2020-12-16 09:36:53 所属栏目:百科 来源:网络整理
导读:/// summary /// 返回XMl文件指定元素的指定属性值 /// /summary /// param name="xmlElement"指定元素/param /// param name="xmlAttribute"指定属性/param /// returns/returns public static bool getXmlValue(string key,out string value) { XmlDocumen
/// <summary> /// 返回XMl文件指定元素的指定属性值 /// </summary> /// <param name="xmlElement">指定元素</param> /// <param name="xmlAttribute">指定属性</param> /// <returns></returns> public static bool getXmlValue(string key,out string value) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlName); XmlNode xmlNode = xmlDoc.SelectSingleNode("root"); XmlNodeList xmlList = xmlNode.SelectNodes("data"); foreach (XmlElement temp in xmlList) { if (temp.GetAttribute("key").ToString() == key) { value = temp.GetAttribute("value"); return true; } } value = ""; return false; } /// <summary> /// 设置XMl文件指定元素的指定属性的值 /// </summary> /// <param name="xmlElement">指定元素</param> /// <param name="xmlAttribute">指定属性</param> /// <param name="xmlValue">指定值</param> public static bool setXmlValue(string key,string value) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlName); XmlNode xmlNode = xmlDoc.SelectSingleNode("root"); XmlNodeList xmlList = xmlNode.SelectNodes("data"); foreach (XmlElement temp in xmlList) { if (temp.GetAttribute("key").ToString() == key) { temp.SetAttribute("value",value); xmlDoc.Save(xmlName); return true; } } return false; } /// <summary> /// 增加一个属性存储 /// </summary> /// <param name="xmlElement"></param> /// <param name="xmlAttribute"></param> /// <param name="xmlValue"></param> public static bool addValue(string xmlElement,string key,string value) { //XmlDocument xmlDoc = new XmlDocument(); //xmlDoc.Load(xmlName); //XmlElement xmlNode = xmlDoc.CreateElement(xmlElement); //xmlNode.SetAttribute("key",key); //xmlNode.SetAttribute("value",value); //XmlNode xml = xmlDoc.DocumentElement.PrependChild(xmlNode); //xmlDoc.Save(xmlName); bool isExist = false; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlName); XmlNode xmlNode1 = xmlDoc.SelectSingleNode("root"); XmlNodeList xmlList = xmlNode1.SelectNodes("data"); foreach (XmlElement temp in xmlList) { if (temp.GetAttribute("key").ToString() == key) { isExist = true; } } if (!isExist) { XmlElement xmlNode = xmlDoc.CreateElement(xmlElement); xmlNode.SetAttribute("key",key); xmlNode.SetAttribute("value",value); XmlNode xml = xmlDoc.DocumentElement.PrependChild(xmlNode); xmlDoc.Save(xmlName); return true; } return false; } /// <summary> /// 遍历方法 /// </summary> /// <param name="orglist"></param> /// <param name="xmlElement"></param> /// <returns></returns> public static IDictionary<string,string> Search(List<string> orglist) { IDictionary<string,string> dic = new Dictionary<string,string>(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlName); XmlNode xmlNode1 = xmlDoc.SelectSingleNode("root"); XmlNodeList xmlList = xmlNode1.SelectNodes("data"); foreach (XmlElement temp in xmlList) { foreach (string tempOrg in orglist) { if (temp.GetAttribute("key").ToString() == tempOrg) { dic.Add(tempOrg,temp.GetAttribute("value").ToString()); } } } return dic; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |