Set XmlNode Attribute - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Set XmlNode Attribute

Demo Code

// This is free software licensed under the NUnit license. You may
using System.Xml;
using System.Collections.Generic;
using System;/*from w w w .  ja  va2 s. c o m*/

public class Main{
        public static void SetAttribute(XmlNode node, string name, object value)
        {
            bool attrAdded = false;

            XmlAttribute attr = node.Attributes[name];
            if (attr == null)
            {
                attr = node.OwnerDocument.CreateAttribute(name);
                node.Attributes.Append(attr);
                attrAdded = true;
            }

            string valString = value.ToString();
            if (attrAdded || attr.Value != valString)
                attr.Value = valString;
        }
}

Related Tutorials