System.Xml.XmlNamedNodeMap Class

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

System.Xml.XmlNamedNodeMap Member List:

Public Properties
Count Read-only

Gets the number of nodes in the XmlNamedNodeMap.
Public Methods
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 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 Overloaded:
GetNamedItem(string name)

Retrieves an XmlNode specified by name.
GetNamedItem Overloaded:
GetNamedItem(string localName, string namespaceURI)

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.
Item Retrieves the node at the specified index in the XmlNamedNodeMap.
RemoveNamedItem Overloaded:
RemoveNamedItem(string name)

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

Removes a node with the matching XmlNode.LocalName and XmlNode.NamespaceURI.
SetNamedItem Adds an 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.XmlNamedNodeMap Member Details

Property: Count (read-only)
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


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:
~XmlNamedNodeMap();

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

Return to top


Method: 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
)
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
)
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: Item(
   int index
)
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


Overloaded Method: RemoveNamedItem(
   string name
)
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
)
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


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

node

An XmlNode to store in the XmlNamedNodeMap. If a node with that name is already present in the map, it is replaced by the new one.

Return Value:
If the node replaces an existing node with the same name, the old node is returned; otherwise, null is returned.
Exceptions
Exception Type Condition
ArgumentException The node was created from a different XmlDocument than the one that created the XmlNamedNodeMap; or the XmlNamedNodeMap is read-only.
Example
The following example uses the XmlAttributeCollection class (which inherits from XmlNamedNodeMap) to add an attribute to 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' publicationdate='1997'> " +
                       "  <title>Pride And Prejudice</title>" +
                       "</book>");      
 
     XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

     //Add a new attribute to the collection.
     XmlAttribute attr = doc.CreateAttribute("style");
     attr.Value = "hardcover";
     attrColl.SetNamedItem(attr);

     Console.WriteLine("Display the modified XML...");
     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.