語言

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 節點不允許新增子節點前置。

它 XPathNavigator 不支援編輯。

範例

在以下範例中,檔案中第一個pages元素book的子元素清單開頭會加上一個新的contosoBooks.xml子元素。

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>

備註

在子節點前加一個子節點會將新節點加入目前節點子節點清單的開頭。 例如,當某元素存在三個子節點時,前置節點即成為第一個子節點。 若不存在子節點,則會建立一個新的子節點。

命名空間前綴與 URI 值可透過 LookupPrefix or LookupNamespace 方法取得。 例如,以下語法會透過使用作用域內的命名空間 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。

適用於