Update XML Attribute - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

Update XML Attribute

Demo Code

//              Copyright (c) 2006-2017 All Rights reserved                   *
using System.Xml.XPath;
using System.Xml;
using System.IO;/*from w w w .j  a  va2 s  .c o m*/
using System.Globalization;
using System;

public class Main{
        public static void UpdateAttribute(XmlElement element, string attributeName, double newValue)
      {
         UpdateAttribute(element, attributeName, newValue.ToString());
      }
        public static void UpdateAttribute(XmlElement element, string attributeName, Single newValue)
      {
         UpdateAttribute(element, attributeName, newValue.ToString());
      }
        public static void UpdateAttribute(XmlElement element, string attributeName, int newValue)
      {
         UpdateAttribute(element, attributeName, newValue.ToString());
      }
        public static void UpdateAttribute(XmlElement element, string attributeName, bool newValue)
      {
         UpdateAttribute(element, attributeName, newValue.ToString().ToLower());
      }
        public static void UpdateAttribute(XmlElement element, string attributeName, string newValue)
      {
         XmlAttribute attrTemp = null;
         attrTemp = (XmlAttribute)element.Attributes.GetNamedItem(attributeName);
         if (attrTemp == null)
            throw new Exception("The attribute was not found!");
         attrTemp.InnerText = newValue;
      }
}

Related Tutorials