XmlNamedNodeMap.SetNamedItem(XmlNode) 方法      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
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
	参数
- node
 - XmlNode
 
要存储在 XmlNode 中的 XmlNamedNodeMap。 如果具有该名称的节点已存在于映射中,则用新节点将其替换。
返回
如果 node 替换具有相同名称的现有节点,则返回旧节点;否则返回 null。
例外
node 是从不同于创建此 XmlNamedNodeMap 的 XmlDocument 创建的;或者 XmlNamedNodeMap 是只读的。
示例
以下示例使用 XmlAttributeCollection 从 XmlNamedNodeMap) 继承的类 (将属性添加到集合。
#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