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

定義

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

public:
 virtual void InsertElementBefore(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void InsertElementBefore (string prefix, string localName, string namespaceURI, string value);
public virtual void InsertElementBefore (string? prefix, string localName, string? namespaceURI, string? value);
abstract member InsertElementBefore : string * string * string * string -> unit
override this.InsertElementBefore : string * string * string * string -> unit
Public Overridable Sub InsertElementBefore (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 檔案之第一個 price 項目的 book 項目子系之前,插入新的 contosoBooks.xml 項目。

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->InsertElementBefore(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.InsertElementBefore(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.InsertElementBefore(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.InsertElementBefore(navigator.Prefix, "pages", LookupNamespaceURI(navigator.Prefix), String.Empty)  

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

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

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

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

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

適用於