Adds a new attribute to the given target element. : Attribute « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » XML » AttributeScreenshots 
Adds a new attribute to the given target element.
 

/*
 * RegExpress
 
 * Copyright (c) 2010, Daniel McGaughran
 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 
 *   http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 
 */


using System;
using System.Xml;


namespace RegExpressModel.Utility
{
  static class XmlUtility
  {

    /// <summary>
    /// Adds a new attribute to the given target element.
    /// </summary>
    /// <param name="targetElement">The target element to add the new attribute to.</param>
    /// <param name="targetDocument">The target document.</param>
    /// <param name="attrName">Name of the attribute.</param>
    /// <param name="value">The value to set for the attribute.</param>
    internal static void AddAttribute(this XmlElement targetElement, XmlDocument targetDocument,
        string attrName, string value)
    {
      if (targetElement == null || targetDocument == null || String.IsNullOrEmpty(attrName))
        return;

      XmlAttribute attr = targetDocument.CreateAttribute(attrName);
      attr.Value = value;

      targetElement.Attributes.Append(attr);
    }



  }
}

   
  
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.Create and set attribute
30.Get Text Attribute
31.Get Int Attribute
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.