Create Or Update XML Attribute - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Create Or Update XML Attribute

Demo Code


using System.Collections;
using System.Configuration;
using System.Xml;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* w w w.  ja  va 2s . c  om*/

public class Main{
        //?????????
        public static bool CreateOrUpdateAttribute(string fileName, string xPath, Dictionary<string, string> attrs, string InnerText)
        {
            bool success = false;
            XmlDocument document = new XmlDocument();

            try
            {
                document.Load(fileName);
                XmlNode node = document.SelectSingleNode(xPath);

                if (node != null)
                {
                    XmlAttributeCollection attributes = node.Attributes;
                    if (attributes != null)
                    {
                        foreach (var attr in attrs)
                        {
                            if (attributes[attr.Key] != null)
                            {
                                //???? ??
                                attributes[attr.Key].Value = attr.Value;
                            }
                            else
                            {
                                //????? ??
                                XmlAttribute atr = document.CreateAttribute(attr.Key);
                                atr.Value = attr.Value;
                                node.Attributes.Append(atr);
                            }
                        }
                    }
                    else
                    {
                        foreach (var attr in attrs)
                        {
                            XmlAttribute atr = document.CreateAttribute(attr.Key);
                            atr.Value = attr.Value;
                            node.Attributes.Append(atr);
                        }
                    }
                    if (!string.IsNullOrEmpty(InnerText))
                    {
                        node.InnerText = InnerText;
                    }
                }
                document.Save(fileName);
                success = true;
            }
            catch (XmlException ex)
            {
                success = false;
                throw ex;
            }

            return success;
        }
        //?????????
        public static bool CreateOrUpdateAttribute(string fileName, string xPath, Dictionary<string, string> attrs)
        {
            bool success = false;
            XmlDocument document = new XmlDocument();

            try
            {
                document.Load(fileName);
                XmlNode node = document.SelectSingleNode(xPath);

                if (node != null)
                {
                    XmlAttributeCollection attributes = node.Attributes;
                    if (attributes != null)
                    {
                        foreach (var attr in attrs)
                        {
                            if (attributes[attr.Key] != null)
                            {
                                //???? ??
                                attributes[attr.Key].Value = attr.Value;
                            }
                            else
                            {
                                //????? ??
                                XmlAttribute atr = document.CreateAttribute(attr.Key);
                                atr.Value = attr.Value;
                                node.Attributes.Append(atr);
                            }
                        }
                    }
                    else
                    {
                        foreach (var attr in attrs)
                        {
                            XmlAttribute atr = document.CreateAttribute(attr.Key);
                            atr.Value = attr.Value;
                            node.Attributes.Append(atr);
                        }
                    }
                }
                document.Save(fileName);
                success = true;
            }
            catch (XmlException ex)
            {
                success = false;
                throw ex;
            }

            return success;
        }
        ////johnny::???
        //public static bool CreateOrUpdateXmlNode(string fileName, string xParentPath,string xPath,XmlNode createnode)
        //{
        //    bool success = false;
        //    bool exsit = false;//???????????
        //    XmlDocument document = new XmlDocument();

        //    try
        //    {
        //        document.Load(fileName);

        //        //???
        //        XmlNode parentNode = document.SelectSingleNode(xParentPath);

        //        if (parentNode != null)
        //        {
        //            XmlNode node = document.SelectSingleNode(xPath);
        //            if (node!=null)
        //            {
        //                XmlAttributeCollection attributes = node.Attributes;
        //                if (attributes != null)
        //                {
        //                    foreach (XmlAttribute attr in createnode.Attributes)
        //                    {
        //                        if (attributes[attr.Name] != null)
        //                        {
        //                            //???? ??
        //                            attributes[attr.Name].Value = attr.Value;
        //                        }
        //                        else
        //                        {
        //                            //????? ??
        //                            node.Attributes.Append(attr);
        //                        }
        //                    }
        //                }
        //                else
        //                {
        //                    foreach (XmlAttribute attr in createnode.Attributes)
        //                    {
        //                        node.Attributes.Append(attr);
        //                    }
        //                }


        //                while (node.HasChildNodes)
        //                {
        //                    for (int i = 0; i < node.ChildNodes.Count; i++)
        //                    {

        //                    }
        //                }
        //            }
        //            else
        //            {

        //            }
        //        }

        //        success = true;
        //    }
        //    catch (XmlException ex)
        //    {
        //        success = false;
        //        throw ex;

        //    }
        //    return success;
        //}


        /// <summary>
        /// ?????????
        /// </summary>
        /// <param name="fileName">XML?????????(????)</param>
        /// <param name="xPath">????????XPath??</param>
        /// <param name="name">??????????</param>
        /// <param name="value">?????????</param>
        /// <returns>????True,????Fasle</returns>
        public static bool CreateOrUpdateAttribute(string fileName, string xPath, string name, string value)
        {
            bool success = false;
            XmlDocument document = new XmlDocument();

            try
            {
                document.Load(fileName);
                XmlNode node = document.SelectSingleNode(xPath);

                if (node != null)
                {
                    XmlAttributeCollection attributes = node.Attributes;
                    if (attributes != null)
                    {
                        if (attributes[name] != null)
                        {
                            //???? ??
                            attributes[name].Value = value;
                        }
                        else
                        {
                            //????? ??
                            XmlAttribute atr = document.CreateAttribute(name);
                            atr.Value = value;
                            node.Attributes.Append(atr);
                        }
                    }
                    else
                    {
                        //????? ??
                        XmlAttribute atr = document.CreateAttribute(name);
                        atr.Value = value;
                        node.Attributes.Append(atr);
                    }
                }
                document.Save(fileName);
                success = true;
            }
            catch (XmlException ex)
            {
                success = false;
                throw ex;
            }

            return success;
        }
}

Related Tutorials