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 (如果有)。 Emptynull 相等。

value
String

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

例外狀況

XPathNavigator 所在的目前節點不是根節點或項目節點。

範例

在下列範例中,會將新的 pages 項目子系附加至 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->AppendChildElement(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.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>  

備註

附加子節點會將新節點新增至目前節點之子節點清單的結尾。 例如,當元素節點存在三個子節點時,附加的節點會變成第四個子節點。 如果沒有子節點存在,則會建立新的子節點。

您可以使用 方法或 LookupNamespace 方法取得命名空間前置詞和 URI 值 LookupPrefix 。 例如,下列語法會使用範圍內命名空間 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

適用於