XmlNamedNodeMap.SetNamedItem(XmlNode) Method

Definition

Adds an XmlNode using its Name property.

public:
 virtual System::Xml::XmlNode ^ SetNamedItem(System::Xml::XmlNode ^ node);
public virtual System.Xml.XmlNode SetNamedItem (System.Xml.XmlNode node);
public virtual System.Xml.XmlNode? SetNamedItem (System.Xml.XmlNode? node);
abstract member SetNamedItem : System.Xml.XmlNode -> System.Xml.XmlNode
override this.SetNamedItem : System.Xml.XmlNode -> System.Xml.XmlNode
Public Overridable Function SetNamedItem (node As XmlNode) As XmlNode

Parameters

node
XmlNode

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.

Returns

If the node replaces an existing node with the same name, the old node is returned; otherwise, null is returned.

Exceptions

The node was created from a different XmlDocument than the one that created the XmlNamedNodeMap; or the XmlNamedNodeMap is read-only.

Examples

The following example uses the XmlAttributeCollection class (which inherits from XmlNamedNodeMap) to add an attribute to the collection.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew 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 );
}
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);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book genre='novel' publicationdate='1997'> " & _
                "  <title>Pride And Prejudice</title>" & _
                "</book>")
                         
    Dim attrColl as XmlAttributeCollection = doc.DocumentElement.Attributes

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

    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.OuterXml)
    
  end sub
end class

Applies to