語言

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

定義

在當前節點子節點清單的末尾建立一個新的子元素節點,使用命名空間前綴、本地名稱及指定值的命名空間 URI。

public:
 virtual void AppendChildElement(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void AppendChildElement(string? prefix, string localName, string? namespaceURI, string? value);
public virtual void AppendChildElement(string prefix, string localName, string namespaceURI, string value);
abstract member AppendChildElement : string * string * string * string -> unit
override this.AppendChildElement : string * string * string * string -> unit
Public Overridable Sub AppendChildElement (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.AppendChildElement(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.AppendChildElement(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 方法或 方法 LookupNamespace 取得。 例如,以下語法透過使用作用域內命名空間 xmlns:bk=http://www.contoso.com/books來附加子元素:

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

這會產生新的子 <bk:pages/> 元素。

以下是使用此 AppendChildElement 方法時需注意的重要事項。

  • 若指定的命名空間前綴為或 nullString.Empty,則新元素的命名空間 URI 前綴可從作用域內的現有命名空間取得。 如果在當前範圍內指定的命名空間 URI 沒有指定命名空間的前綴,則會自動產生命名空間前綴。

  • AppendChildElement此方法僅在 被XPathNavigator置於根節點或元素節點時有效。

  • 此 AppendChildElement 方法不影響 的位置 XPathNavigator。

適用於