Add Or Update XML Attribute - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Add Or Update XML Attribute

Demo Code


using System.Xml;
using System.Collections.Generic;
using System;/*w  w w.ja  v a  2s. com*/

public class Main{
        public static void AddOrUpdateAttribute(XmlNode node, string attributeName, string value, IComparer<XmlNode> nodeOrderComparer)
      {
         XmlNode attr = node.Attributes.GetNamedItem(attributeName);
         if (attr == null)
         {
            attr = GetDocument(node).CreateAttribute(attributeName);
            if (nodeOrderComparer == null)
            {
               node.Attributes.SetNamedItem(attr);
            }
            else
            {
               InsertNodeUsingDefinedOrder(node, attr, nodeOrderComparer);
            }
         }
         attr.Value = value;
      }
        public static void AddOrUpdateAttribute(XmlNode node, string attributeName, string value)
      {
         AddOrUpdateAttribute(node, attributeName, value, null);
      }
}

Related Tutorials