Прочетете на английски Редактиране

Споделяне чрез


XmlNamedNodeMap.SetNamedItem(XmlNode) Method

Definition

Adds an XmlNode using its Name property.

C#
public virtual System.Xml.XmlNode SetNamedItem(System.Xml.XmlNode node);
C#
public virtual System.Xml.XmlNode? SetNamedItem(System.Xml.XmlNode? node);

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.

C#
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);
  }
}

Applies to

Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0