XPathNavigator.InsertElementAfter(String, String, String, String) 方法

定義

使用命名空間前置詞、區域名稱以及有指定值的指定命名空間 URI,在目前的節點之後建立新的同層級項目。

public:
 virtual void InsertElementAfter(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void InsertElementAfter (string prefix, string localName, string namespaceURI, string value);
public virtual void InsertElementAfter (string? prefix, string localName, string? namespaceURI, string? value);
abstract member InsertElementAfter : string * string * string * string -> unit
override this.InsertElementAfter : string * string * string * string -> unit
Public Overridable Sub InsertElementAfter (prefix As String, localName As String, namespaceURI As String, value As String)

參數

prefix
String

新的子項目的命名空間前置詞 (如果有的話)。

localName
String

新的子項目的區域名稱 (如果有的話)。

namespaceURI
String

新子項目的命名空間 URI (如果有)。 Empty 等於 null

value
String

新的子項目的值。 如果已傳遞 Emptynull,則會建立空白項目。

例外狀況

XPathNavigator 的位置不允許在目前節點的後面插入新的同層級節點。

範例

在下列範例中,會將新 pages 元素插入檔案中 contosoBooks.xml 第一個專案 book 的子專案後面 price

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");
navigator->MoveToChild("price", "http://www.contoso.com/books");

navigator->InsertElementAfter(navigator->Prefix, "pages", navigator->LookupNamespace(navigator->Prefix), "100");

navigator->MoveToParent();
Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");
navigator.MoveToChild("price", "http://www.contoso.com/books");

navigator.InsertElementAfter(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100");

navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")
navigator.MoveToChild("price", "http://www.contoso.com/books")

navigator.InsertElementAfter(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100")

navigator.MoveToParent()
Console.WriteLine(navigator.OuterXml)

範例將 contosoBooks.xml 檔案做為輸入。

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

備註

您可以使用 或 LookupNamespace 方法來取得 LookupPrefix 命名空間前置詞和 URI 值。 例如,下列語法會使用範圍內的命名空間 xmlns:bk="http://www.contoso.com/books" 來插入同層級專案:

navigator.InsertElementAfter(navigator.Prefix, "pages", LookupNamespaceURI(navigator.Prefix), String.Empty)  

這會建立新的同層級 <bk:pages/> 專案。

以下是使用 InsertElementAfter 方法時要考慮的重要注意事項。

  • 如果指定的命名空間前置詞為 nullString.Empty ,則新元素之命名空間 URI 的前置詞會從範圍中的目前命名空間取得。 如果目前範圍沒有指派給指定命名空間 URI 的命名空間前置詞,則會自動產生命名空間前置詞。

  • 只有在 位於專案、文字、處理指示或批註節點的 時 XPathNavigator ,方法 InsertElementAfter 才有效。

  • 方法 InsertElementAfter 不會影響 的位置 XPathNavigator

適用於