XmlAttributeCollection.InsertAfter(XmlAttribute, XmlAttribute) 方法

定義

在指定的參考屬性之後立即插入指定的屬性。

public:
 System::Xml::XmlAttribute ^ InsertAfter(System::Xml::XmlAttribute ^ newNode, System::Xml::XmlAttribute ^ refNode);
public:
 virtual System::Xml::XmlAttribute ^ InsertAfter(System::Xml::XmlAttribute ^ newNode, System::Xml::XmlAttribute ^ refNode);
public System.Xml.XmlAttribute InsertAfter(System.Xml.XmlAttribute newNode, System.Xml.XmlAttribute refNode);
public virtual System.Xml.XmlAttribute InsertAfter(System.Xml.XmlAttribute newNode, System.Xml.XmlAttribute refNode);
member this.InsertAfter : System.Xml.XmlAttribute * System.Xml.XmlAttribute -> System.Xml.XmlAttribute
abstract member InsertAfter : System.Xml.XmlAttribute * System.Xml.XmlAttribute -> System.Xml.XmlAttribute
override this.InsertAfter : System.Xml.XmlAttribute * System.Xml.XmlAttribute -> System.Xml.XmlAttribute
Public Function InsertAfter (newNode As XmlAttribute, refNode As XmlAttribute) As XmlAttribute
Public Overridable Function InsertAfter (newNode As XmlAttribute, refNode As XmlAttribute) As XmlAttribute

參數

newNode
XmlAttribute

要插入的屬性。

refNode
XmlAttribute

參考屬性。 newNode 位於 後面 refNode

傳回

XmlAttribute 插入收藏。

例外狀況

它是 newNode 根據一份與創建本收藏的文件不同的文件製作而成。 或者他 refNode 不是這個收藏的成員。

範例

以下範例為文件新增一個屬性。

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main(){

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create a new attribute.
    XmlAttribute newAttr = doc.CreateAttribute("genre");
    newAttr.Value = "novel";

    //Create an attribute collection and add the new attribute
    //to the collection.
    XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;
    attrColl.InsertAfter(newAttr, attrColl[0]);

    Console.WriteLine("Display the modified XML...\r\n");
    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 ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")      

    'Create a new attribute.
    Dim newAttr as XmlAttribute = doc.CreateAttribute("genre")
    newAttr.Value = "novel"

    'Create an attribute collection and add the new attribute
    'to the collection.  
    Dim attrColl as XmlAttributeCollection = doc.DocumentElement.Attributes
    attrColl.InsertAfter(newAttr, attrColl.ItemOf(0))

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

  end sub
end class

備註

如果集合中已有同名屬性,原始屬性會從集合中移除並 newNode 插入集合中。 若 refNodenullnewNode 則 會入集合的開頭。

此方法是 Microsoft 對文件物件模型(DOM)的擴充。

適用於