System.Xml.XmlAttributeCollection Class

Assembly: System.Xml.dll
Namespace: System.Xml
Summary
Represents a collection of attributes that can be accessed by name or index.
C# Syntax:
public class XmlAttributeCollection : XmlNamedNodeMap, ICollection
See also:
System.Xml Namespace

System.Xml.XmlAttributeCollection Member List:

Public Properties
Count
(inherited from System.Xml.XmlNamedNodeMap)
Read-only

See base class member description: System.Xml.XmlNamedNodeMap.Count


Gets the number of nodes in the XmlNamedNodeMap.
ItemOf Read-only

Overloaded:
ItemOf

Gets the attribute with the specified name.
ItemOf Read-only

Overloaded:
ItemOf

Gets the attribute with the specified index.
ItemOf Read-only

Overloaded:
ItemOf

Gets the attribute with the specified local name and namespace URI.
Public Methods
Append Inserts the specified attribute as the last node in the collection.
CopyTo Copies all the XmlAttribute objects from this collection into the given array.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetEnumerator
(inherited from System.Xml.XmlNamedNodeMap)
See base class member description: System.Xml.XmlNamedNodeMap.GetEnumerator


Provides support for the "foreach" style iteration over the collection of nodes in the XmlNamedNodeMap.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetNamedItem
(inherited from System.Xml.XmlNamedNodeMap)
Overloaded:
GetNamedItem(string name)

See base class member description: System.Xml.XmlNamedNodeMap.GetNamedItem


Retrieves an XmlNode specified by name.
GetNamedItem
(inherited from System.Xml.XmlNamedNodeMap)
Overloaded:
GetNamedItem(string localName, string namespaceURI)

See base class member description: System.Xml.XmlNamedNodeMap.GetNamedItem


Retrieves a node with the matching XmlNode.LocalName and XmlNode.NamespaceURI.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
InsertAfter Inserts the specified attribute immediately after the specified reference attribute.
InsertBefore Inserts the specified attribute immediately before the specified reference attribute.
Item
(inherited from System.Xml.XmlNamedNodeMap)
See base class member description: System.Xml.XmlNamedNodeMap.Item


Retrieves the node at the specified index in the XmlNamedNodeMap.
Prepend Inserts the specified attribute as the first node in the collection.
Remove Removes the specified attribute from the collection.
RemoveAll Removes all attributes from the collection.
RemoveAt Removes the attribute corresponding to the specified index from the collection.
RemoveNamedItem
(inherited from System.Xml.XmlNamedNodeMap)
Overloaded:
RemoveNamedItem(string name)

See base class member description: System.Xml.XmlNamedNodeMap.RemoveNamedItem


Removes the node from the XmlNamedNodeMap.
RemoveNamedItem
(inherited from System.Xml.XmlNamedNodeMap)
Overloaded:
RemoveNamedItem(string localName, string namespaceURI)

See base class member description: System.Xml.XmlNamedNodeMap.RemoveNamedItem


Removes a node with the matching XmlNode.LocalName and XmlNode.NamespaceURI.
SetNamedItem Overridden:
Adds a XmlNode using its XmlNode.Name property
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.XmlAttributeCollection Member Details

Property: Count (read-only)
Inherited
See base class member description: System.Xml.XmlNamedNodeMap.Count

Summary
Gets the number of nodes in the XmlNamedNodeMap.
C# Syntax:
public virtual int Count {get;}
Example
The following example uses the XmlAttributeCollection class (which inherits from XmlNamedNodeMap) to display all the attributes of a book.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<book genre='novel' publicationdate='1997'> " +
                       "  <title>Pride And Prejudice</title>" +
                       "</book>");      
 
     XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

     Console.WriteLine("Display all the attributes for this book...");
     for (int i=0; i < attrColl.Count; i++)
     {
        Console.WriteLine("{0} = {1}", attrColl.Item(i).Name, attrColl.Item(i).Value);
     }         
    
  }
}

    

Return to top


Overloaded Property: ItemOf (read-only)
Summary
Gets the attribute with the specified name.
C# Syntax:
No member name public virtual XmlAttribute this[string name] {get;}
Parameters:

name

The qualified name of the attribute.

Remarks
This property is a Microsoft extension to the Document Object Model (DOM). It is equivalent to calling XmlNamedNodeMap.GetNamedItem.
Example
The following example removes an attribute from the document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create an attribute collection and remove an attribute
    //from the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.Remove(attrColl["genre"]);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);  
  }
}

    

Return to top


Overloaded Property: ItemOf (read-only)
Summary
Gets the attribute with the specified index.
C# Syntax:
No member name public virtual XmlAttribute this[int i] {get;}
Parameters:

i

The index of the attribute.

Remarks
This property is a Microsoft extension to the Document Object Model (DOM). It is equivalent to calling XmlNamedNodeMap.Item.
Example
The following example displays all the attributes in the collection.


using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create an attribute collection. 
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

    Console.WriteLine("Display all the attributes in the collection...\r\n");
    for (int i=0; i < attrColl.Count; i++)
    {
      Console.Write("{0} = ", attrColl[i].Name);
      Console.Write("{0}", attrColl[i].Value);
      Console.WriteLine();
    }           
  }
}

    
See also:
XmlNamedNodeMap.Item

Return to top


Overloaded Property: ItemOf (read-only)
Summary
Gets the attribute with the specified local name and namespace URI.
C# Syntax:
No member name public virtual XmlAttribute this[string localName, string namespaceURI] {get;}
Parameters:

localName

The local name of the attribute.

namespaceURI

The namespace URI of the attribute.

Remarks
This property is a Microsoft extension to the Document Object Model (DOM). It is equivalent to calling XmlNamedNodeMap.GetNamedItem.

Return to top


Method: Append(
   XmlAttribute node
)
Summary
Inserts the specified attribute as the last node in the collection.
C# Syntax:
public virtual XmlAttribute Append(
   XmlAttribute node
);
Parameters:

node

The XmlAttribute to insert.

Return Value:
The XmlAttribute to append to the collection.
Exceptions
Exception Type Condition
ArgumentException node was created from a document different from the one that created this collection.
Remarks
If an attribute with the same name is already present in the collection, the original attribute is removed from the collection and node is added to the end of the collection.

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example adds a new attribute to a document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create a new attribute.
    XmlAttribute newAttr = doc.CreateAttribute("genre");
    newAttr.Value = "novel";

    //Create an attribute collection and add the new attribute
    //to the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.Append(newAttr);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);  
  }
}

    

Return to top


Method: CopyTo(
   XmlAttribute[] array,
   int index
)
Summary
Copies all the XmlAttribute objects from this collection into the given array.
C# Syntax:
public void CopyTo(
   XmlAttribute[] array,
   int index
);
Parameters:

array

The array that is the destination of the objects copied from this collection.

index

The index in array where copying begins.

Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example uses CopyTo to copy all the attributes in the collection into an array.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create an attribute collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

    //Declare the array.
    XmlAttribute[] array = new XmlAttribute[2];
    int index=0;

    //Copy all the attributes into the array.
    attrColl.CopyTo(array, index);

    Console.WriteLine("Display all the attributes in the array..");
    foreach (XmlAttribute attr in array){
      Console.WriteLine("{0} = {1}",attr.Name,attr.Value);
    } 
  
  }
}

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlAttributeCollection();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetEnumerator()
Inherited
See base class member description: System.Xml.XmlNamedNodeMap.GetEnumerator

Summary
Provides support for the "foreach" style iteration over the collection of nodes in the XmlNamedNodeMap.
C# Syntax:
public virtual IEnumerator GetEnumerator();
Return Value:
An IEnumerator.
Implements:
IEnumerable.GetEnumerator
Example
The following example displays all attributes in the collection.

 
 using System;
 using System.IO;
 using System.Xml;
 using System.Collections;
 
 public class Sample
 {
   public static void Main()
   {

       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book genre='novel' publicationdate='1997' " +
                         "      ISBN='1-861001-57-5'>" +
                         "  <title>Pride And Prejudice</title>" +
                         "</book>");      
 
       XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

       Console.WriteLine("Display all the attributes for this book...");
       IEnumerator ienum = attrColl.GetEnumerator();
       while (ienum.MoveNext())
       {
         XmlAttribute attr = (XmlAttribute)ienum.Current;
         Console.WriteLine("{0} = {1}", attr.Name, attr.Value);
       }   
   } 
 }

    

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: GetNamedItem(
   string name
)
Inherited
See base class member description: System.Xml.XmlNamedNodeMap.GetNamedItem

Summary
Retrieves an XmlNode specified by name.
C# Syntax:
public virtual XmlNode GetNamedItem(
   string name
);
Parameters:

name

The qualified name of the node to retrieve. It is matched against the XmlNode.Name property of the matching node.

Return Value:
An XmlNode with the specified name or null if a matching node is not found.
Example
The following example uses the XmlAttributeCollection class (which inherits from XmlNamedNodeMap) to modify an attribute.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<book genre='novel' publicationdate='1997'> " +
                       "  <title>Pride And Prejudice</title>" +
                       "</book>");      
 
     XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

     //Change the value for the genre attribute.
     XmlAttribute attr = (XmlAttribute)attrColl.GetNamedItem("genre");
     attr.Value = "fiction";

     Console.WriteLine("Display the modified XML...");
     Console.WriteLine(doc.OuterXml);
    
  }
}

    

Return to top


Overloaded Method: GetNamedItem(
   string localName,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlNamedNodeMap.GetNamedItem

Summary
Retrieves a node with the matching XmlNode.LocalName and XmlNode.NamespaceURI.
C# Syntax:
public virtual XmlNode GetNamedItem(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name of the node to retrieve.

namespaceURI

The namespace URI of the node to retrieve.

Return Value:
An XmlNode with the matching local name and namespace URI or null if a matching node was not found.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InsertAfter(
   XmlAttribute newNode,
   XmlAttribute refNode
)
Summary
Inserts the specified attribute immediately after the specified reference attribute.
C# Syntax:
public virtual XmlAttribute InsertAfter(
   XmlAttribute newNode,
   XmlAttribute refNode
);
Parameters:

newNode

The XmlAttribute to insert.

refNode

The XmlAttribute that is the reference attribute.newNode is placed after the refNode.

Return Value:
The XmlAttribute to insert into the collection.
Exceptions
Exception Type Condition
ArgumentException The newNode was created from a document different from the one that created this collection. Or the refNode is not a member of this collection.
Remarks
If an attribute with the same name is already present in the collection, the original attribute is removed from the collection and newNode is inserted into the collection. If refNode is null, newNode is inserted at the beginning of the collection.

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example adds a new attribute to a document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create a new attribute.
    XmlAttribute newAttr = doc.CreateAttribute("genre");
    newAttr.Value = "novel";

    //Create an attribute collection and add the new attribute
    //to the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.InsertAfter(newAttr, attrColl[0]);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);  
  }
}

    

Return to top


Method: InsertBefore(
   XmlAttribute newNode,
   XmlAttribute refNode
)
Summary
Inserts the specified attribute immediately before the specified reference attribute.
C# Syntax:
public virtual XmlAttribute InsertBefore(
   XmlAttribute newNode,
   XmlAttribute refNode
);
Parameters:

newNode

The XmlAttribute to insert.

refNode

The XmlAttribute that is the reference attribute.newNode is placed before the refNode.

Return Value:
The XmlAttribute to insert into the collection.
Exceptions
Exception Type Condition
ArgumentException The newNode was created from a document different from the one that created this collection. Or the refNode is not a member of this collection.
Remarks
If an attribute with the same name is already present in the collection, the original attribute is removed from the collection and newNode is inserted into the collection. If refNode is null, newNode is inserted at the end of the collection.

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example adds a new attribute to a document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create a new attribute.
    XmlAttribute newAttr = doc.CreateAttribute("genre");
    newAttr.Value = "novel";

    //Create an attribute collection and add the new attribute
    //to the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.InsertBefore(newAttr, attrColl[0]);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);  
  }
}

    

Return to top


Method: Item(
   int index
)
Inherited
See base class member description: System.Xml.XmlNamedNodeMap.Item

Summary
Retrieves the node at the specified index in the XmlNamedNodeMap.
C# Syntax:
public virtual XmlNode Item(
   int index
);
Parameters:

index

The index position of the node to retrieve from the XmlNamedNodeMap. The index is zero-based; therefore, the index of the first node is 0 and the index of the last node is XmlNamedNodeMap.Count -1.

Return Value:
The XmlNode at the specified index. If index is less than 0 or greater than or equal to the XmlNamedNodeMap.Count property, null is returned.
Example
The following example uses the XmlAttributeCollection class (which inherits from XmlNamedNodeMap) to display all the attributes of a book.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<book genre='novel' publicationdate='1997'> " +
                       "  <title>Pride And Prejudice</title>" +
                       "</book>");      
 
     XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

     Console.WriteLine("Display all the attributes for this book...");
     for (int i=0; i < attrColl.Count; i++)
     {
        Console.WriteLine("{0} = {1}", attrColl.Item(i).Name, attrColl.Item(i).Value);
     }         
    
  }
}

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Prepend(
   XmlAttribute node
)
Summary
Inserts the specified attribute as the first node in the collection.
C# Syntax:
public virtual XmlAttribute Prepend(
   XmlAttribute node
);
Parameters:

node

The XmlAttribute to insert.

Return Value:
The XmlAttribute added to the collection.
Remarks
If an attribute with the same name is already present in the collection, the original attribute is removed from the collection and node is added to the beginning of the collection.

This method is a Microsoft extension to the Document Object Model (DOM).

Example
The following example adds a new attribute to a document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create a new attribute.
    XmlAttribute newAttr = doc.CreateAttribute("genre");
    newAttr.Value = "novel";

    //Create an attribute collection and add the new attribute
    //to the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.Prepend(newAttr);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);  
  }
}

    

Return to top


Method: Remove(
   XmlAttribute node
)
Summary
Removes the specified attribute from the collection.
C# Syntax:
public virtual XmlAttribute Remove(
   XmlAttribute node
);
Parameters:

node

The XmlAttribute to remove.

Return Value:
The node removed or null if it is not found in the collection.
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example removes an attribute from the document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create an attribute collection and remove an attribute
    //from the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.Remove(attrColl["genre"]);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);  
  }
}

    

Return to top


Method: RemoveAll()
Summary
Removes all attributes from the collection.
C# Syntax:
public virtual void RemoveAll();
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example removes all attributes from the document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create an attribute collection and remove all attributes
    //from the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.RemoveAll();

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);
  
  }
}

    

Return to top


Method: RemoveAt(
   int i
)
Summary
Removes the attribute corresponding to the specified index from the collection.
C# Syntax:
public virtual XmlAttribute RemoveAt(
   int i
);
Parameters:

i

The index of the node to remove. The first node has index 0.

Return Value:
Returns null if there is no attribute at the specified index.
Remarks
This method is a Microsoft extension to the Document Object Model (DOM).
Example
The following example removes an attribute from the document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create an attribute collection and remove an attribute
    //from the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.RemoveAt(0);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);  
  }
}

    

Return to top


Overloaded Method: RemoveNamedItem(
   string name
)
Inherited
See base class member description: System.Xml.XmlNamedNodeMap.RemoveNamedItem

Summary
Removes the node from the XmlNamedNodeMap.
C# Syntax:
public virtual XmlNode RemoveNamedItem(
   string name
);
Parameters:

name

The qualified name of the node to remove. The name is matched against the XmlNode.Name property of the matching node.

Return Value:
The XmlNode removed from this XmlNamedNodeMap or null if a matching node was not found.
Example
The following example uses the XmlAttributeCollection class (which inherits from XmlNamedNodeMap) to remove an attribute.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<book genre='novel' publicationdate='1997'> " +
                       "  <title>Pride And Prejudice</title>" +
                       "</book>");      
 
     XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

     //Remove the publicationdate attribute.
     attrColl.RemoveNamedItem("publicationdate");

     Console.WriteLine("Display the modified XML...");
     Console.WriteLine(doc.OuterXml);
    
  }
}

    

Return to top


Overloaded Method: RemoveNamedItem(
   string localName,
   string namespaceURI
)
Inherited
See base class member description: System.Xml.XmlNamedNodeMap.RemoveNamedItem

Summary
Removes a node with the matching XmlNode.LocalName and XmlNode.NamespaceURI.
C# Syntax:
public virtual XmlNode RemoveNamedItem(
   string localName,
   string namespaceURI
);
Parameters:

localName

The local name of the node to remove.

namespaceURI

The namespace URI of the node to remove.

Return Value:
The XmlNode removed or null if a matching node was not found.

Return to top


Overridden Method: SetNamedItem(
   XmlNode node
)
Summary
Adds a XmlNode using its XmlNode.Name property
C# Syntax:
public override XmlNode SetNamedItem(
   XmlNode node
);
Parameters:

node

An attribute node to store in this collection. The node will later be accessible using the name of the node. If a node with that name is already present in the collection, it is replaced by the new one; otherwise, the node is appended to the end of the collection.

Return Value:
If the node replaces an existing node with the same name, the old node is returned; otherwise, the added node is returned.
Exceptions
Exception Type Condition
ArgumentException node was created from a different XmlDocument than the one that created this collection.

This XmlAttributeCollection is read-only.

InvalidOperationException node is an XmlAttribute that is already an attribute of another XmlElement object. To re-use attributes in other elements, you must clone the XmlAttribute objects you want to re-use.
Example
The following example adds a new attribute to a document.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){
  
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");      

    //Create a new attribute.
    XmlAttribute newAttr = doc.CreateAttribute("genre");
    newAttr.Value = "novel";

    //Create an attribute collection and add the new attribute
    //to the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.SetNamedItem(newAttr);

    Console.WriteLine("Display the modified XML...\r\n");
    Console.WriteLine(doc.OuterXml);  
  }
}

    

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.