Add Attribute to XmlElement - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Add Attribute to XmlElement

Demo Code

//              Copyright (c) 2006-2017 All Rights reserved                   *
using System.Xml.XPath;
using System.Xml;
using System.IO;//from   w ww  .  j av a  2s.c om
using System.Globalization;
using System;

public class Main{
        public static XmlAttribute AddAttribute(XmlElement element, string name, bool value)
      {
         return AddAttribute((XmlNode)element, name, value);
      }
        public static XmlAttribute AddAttribute(XmlNode element, string name, bool value)
      {
         XmlDocument docOwner = null;
         XmlAttribute attrNew = null;

         docOwner = element.OwnerDocument;
         attrNew = docOwner.CreateAttribute(name);
         //attrNew.InnerText = value.ToString(_cultureProvider);
         if (value) attrNew.InnerText = "1";
         else attrNew.InnerText = "0";
         element.Attributes.Append(attrNew);
         return attrNew;
      }
        public static XmlAttribute AddAttribute(XmlElement node, string name, int value)
      {
         return AddAttribute((XmlNode)node, name, value);
      }
        public static XmlAttribute AddAttribute(XmlNode element, string name, int value)
      {
         var attrNew = element.OwnerDocument.CreateAttribute(name);
         attrNew.InnerText = value.ToString(_cultureProvider);
         element.Attributes.Append(attrNew);
         return attrNew;
      }
        public static XmlAttribute AddAttribute(XmlElement node, string name, double value)
      {
         return AddAttribute((XmlNode)node, name, value);
      }
        public static XmlAttribute AddAttribute(XmlNode element, string name, Guid value)
      {
         var attrNew = element.OwnerDocument.CreateAttribute(name);
         attrNew.InnerText = value.ToString();
         element.Attributes.Append(attrNew);
         return attrNew;
      }
        public static XmlAttribute AddAttribute(XmlNode element, string name, double value)
      {
         var attrNew = element.OwnerDocument.CreateAttribute(name);
         attrNew.InnerText = value.ToString(_cultureProvider);
         element.Attributes.Append(attrNew);
         return attrNew;
      }
        public static XmlAttribute AddAttribute(XmlElement node, string name, string value)
      {
         return AddAttribute((XmlNode)node, name, value);
      }
        public static XmlAttribute AddAttribute(XmlNode node, string name, string value)
      {
         var attrNew = node.OwnerDocument.CreateAttribute(name);
         attrNew.InnerText = value;
         node.Attributes.Append(attrNew);
         return attrNew;
      }
}

Related Tutorials