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

定義

使用命名空間前置詞、區域名稱以及有指定值的指定命名空間 URI,在目前節點的子節點清單開頭建立新的子項目。

public:
 virtual void PrependChildElement(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void PrependChildElement (string prefix, string localName, string namespaceURI, string value);
public virtual void PrependChildElement (string? prefix, string localName, string? namespaceURI, string? value);
abstract member PrependChildElement : string * string * string * string -> unit
override this.PrependChildElement : string * string * string * string -> unit
Public Overridable Sub PrependChildElement (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 個專案之子項目清單的開頭。

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->PrependChildElement(navigator->Prefix, "pages", navigator->LookupNamespace(navigator->Prefix), "100");

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.PrependChildElement(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100");

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.PrependChildElement(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100")

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

這會建立新的子 <bk:pages/> 專案。

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

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

  • 只有在 PrependChildElement 位於專案節點上的 時 XPathNavigator ,方法才有效。

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

適用於