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

新子元素节点的值。 如果 Empty 已传递或 null 传递,则会创建一个空元素。

例外

定位到的当前节点 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>

注解

追加子节点会将新节点添加到当前节点的子节点列表的末尾。 例如,当元素节点存在三个子节点时,追加的节点将成为第四个子节点。 如果不存在子节点,则会创建新的子节点。

可以使用方法或LookupPrefix方法获取LookupNamespace命名空间前缀和 URI 值。 例如,以下语法使用作用域内命名空间 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方法的位置。

适用于