简单XML操作类

前端之家收集整理的这篇文章主要介绍了简单XML操作类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/// <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;
        }
原文链接:https://www.f2er.com/xml/298190.html

猜你在找的XML相关文章