Sets the value of an attribute for a given XmlNode. - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Sets the value of an attribute for a given XmlNode.

Demo Code

// All rights reserved.
using System.Diagnostics;
using System.Collections.Specialized;
using System.Xml;
using System.Data;
using System;/*from w  w w .  j  a va 2  s  .c  om*/

public class Main{
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Sets the value of an attribute for a given XmlNode.
        /// </summary>
        /// <param name="node">XmlNode whose attribute will be set.</param>
        /// <param name="attributeName">Name of the attribute to set.</param>
        /// <param name="value">Value to be set</param>
        /// <returns>True if success.</returns>
        /// -----------------------------------------------------------------------------
        public static bool SetAttributeValue(XmlNode node, string attributeName, UInt32 value)
        {
            bool success = false;
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[attributeName];
                if (attribute != null)
                {
                    attribute.Value = value.ToString();
                    success = true;
                }
            }
            return success;
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Sets the value of an attribute for a given XmlNode.
        /// </summary>
        /// <param name="node">XmlNode whose attribute will be set.</param>
        /// <param name="attributeName">Name of the attribute to set.</param>
        /// <param name="value">Value to be set</param>
        /// <returns>True if success.</returns>
        /// -----------------------------------------------------------------------------
        public static bool SetAttributeValue(XmlNode node, string attributeName, int value)
        {
            bool success = false;
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[attributeName];
                if (attribute != null)
                {
                    attribute.Value = value.ToString();
                    success = true;
                }
            }
            return success;
        }
        #endregion

        #region SetAttributeValue(node, name, value)
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Sets the value of an attribute for a given XmlNode.
        /// </summary>
        /// <param name="node">XmlNode whose attribute will be set.</param>
        /// <param name="attributeName">Name of the attribute to set.</param>
        /// <param name="value">Value to be set</param>
        /// <returns>True if success.</returns>
        /// -----------------------------------------------------------------------------
        public static bool SetAttributeValue(XmlNode node, string attributeName, string value)
        {
            bool success = false;
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[attributeName];
                if (attribute != null)
                {
                    attribute.Value = value;
                    success = true;
                }
            }
            return success;
        }
}

Related Tutorials