Create and set attribute : Attribute « XML « C# / C Sharp






Create and set attribute

 
/*
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * Useful tools for XML.
 */

using System ;
using System.Globalization ;
using System.Xml ;

namespace FredCK.FCKeditorV2
{
  internal sealed class XmlUtil
  {
    private XmlUtil()
    {}

    public static XmlNode AppendElement( XmlNode node, string newElementName )
    {
      return AppendElement( node, newElementName, null ) ;
    }

    public static XmlNode AppendElement( XmlNode node, string newElementName, string innerValue )
    {
      XmlNode oNode ;

      if ( node is XmlDocument )
                oNode = node.AppendChild( ((XmlDocument)node).CreateElement( newElementName ) ) ;
      else
        oNode = node.AppendChild( node.OwnerDocument.CreateElement( newElementName ) ) ;

      if ( innerValue != null )
        oNode.AppendChild( node.OwnerDocument.CreateTextNode( innerValue ) ) ;

      return oNode ;
    }

    public static XmlAttribute CreateAttribute( XmlDocument xmlDocument, string name, string value )
    {
      XmlAttribute oAtt = xmlDocument.CreateAttribute( name ) ;
      oAtt.Value = value ;
      return oAtt ;
    }

    public static void SetAttribute( XmlNode node, string attributeName, string attributeValue )
    {
      if ( node.Attributes[ attributeName ] != null )
        node.Attributes[ attributeName ].Value = attributeValue ;
      else
        node.Attributes.Append( CreateAttribute( node.OwnerDocument, attributeName, attributeValue ) ) ;
    }
  }
}

   
  








Related examples in the same category

1.Set Attribute Value
2.Get Attribute Value
3.Get Int value from xml attribute
4.Get Attribute value (2)
5.Get attribute or return default value
6.Get Bool Attribute Or Throw exception
7.Get attribute value and convert to integer type
8.Get attribute value and convert to integer type or throw exception
9.Get Attribute and return Int64
10.Get Attribute and return Int64 or throw exception
11.Adds an XmlAttribute to a XmlNode
12.Get boolean value if an attribute is 1 or true
13.Attempts to get and cast attribute from an XmlElement
14.Attempts to get and convert an attribute from an XmlElement
15.Gets a string from an attribute or node.
16.Remove all the xml namespaces (xmlns) attributes in the xml string
17.Get Attribute With Conversion
18.Find a single Attribute of the type 'attributeType' from the supplied class
19.Create Xml Attribute, Node
20.Create Attribute, Text Element, Image Element,Read Image Element
21.Attribute Util
22.Find and get Attribute value
23.Finds an attribute with the given name in the given XML node and returns the attribute of the value.
24.Parse Attribute
25.Get attribute value (3)
26.Get int type attribute value
27.Get attribute value or throw exception
28.Read an XML file from the xml element and the xml attribute
29.Adds a new attribute to the given target element.
30.Get Text Attribute
31.Get Int Attribute