共用方式為


XmlNamedNodeMap.SetNamedItem(XmlNode) 方法

定義

使用其 XmlNode 屬性加入 Name

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。 如果具有該名稱的節點已經在對應中,就會以新節點取代。

傳回

XmlNode

如果 node 取代同名的現有節點,則傳回舊節點;否則傳回 null

例外狀況

從不同於建立 XmlNamedNodeMapXmlDocument 來建立 node;否則 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

適用於