XPathNavigator.AppendChild 方法

定义

在当前节点的子节点列表末尾创建新的子节点。

重载

名称 说明
AppendChild()

返回一个 XmlWriter 对象,该对象用于在当前节点的子节点列表的末尾创建一个或多个新的子节点。

AppendChild(String)

使用指定的 XML 数据字符串在当前节点的子节点列表末尾创建新的子节点。

AppendChild(XmlReader)

使用指定的对象的 XML 内容 XmlReader 在当前节点的子节点列表末尾创建新的子节点。

AppendChild(XPathNavigator)

使用指定节点中的节点 XPathNavigator 在当前节点的子节点列表末尾创建新的子节点。

AppendChild()

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

返回一个 XmlWriter 对象,该对象用于在当前节点的子节点列表的末尾创建一个或多个新的子节点。

public:
 virtual System::Xml::XmlWriter ^ AppendChild();
public virtual System.Xml.XmlWriter AppendChild();
abstract member AppendChild : unit -> System.Xml.XmlWriter
override this.AppendChild : unit -> System.Xml.XmlWriter
Public Overridable Function AppendChild () As XmlWriter

返回

一个 XmlWriter 对象,该对象用于在当前节点的子节点列表的末尾创建新的子节点。

例外

定位到的当前节点 XPathNavigator 不是根节点或元素节点。

示例

在下面的示例中,使用pages方法book返回的对象,将新的contosoBooks.xml子元素追加到文件中第一个XmlWriter元素的子元素AppendChild列表中。

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");

XmlWriter pages = navigator.AppendChild();
pages.WriteElementString("pages", "100");
pages.Close();

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")

Dim pages As XmlWriter = navigator.AppendChild()
pages.WriteElementString("pages", "100")
pages.Close()

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>

注解

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

以下是使用 AppendChild 该方法时要考虑的重要说明。

可以将多个节点写入编写器。 所有节点都追加到当前节点的子节点列表的末尾。

适用于

AppendChild(String)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

使用指定的 XML 数据字符串在当前节点的子节点列表末尾创建新的子节点。

public:
 virtual void AppendChild(System::String ^ newChild);
public virtual void AppendChild(string newChild);
abstract member AppendChild : string -> unit
override this.AppendChild : string -> unit
Public Overridable Sub AppendChild (newChild As String)

参数

newChild
String

新子节点的 XML 数据字符串。

例外

XML 数据字符串参数为 null.

定位到的当前节点 XPathNavigator 不是根节点或元素节点。

XML 数据字符串参数格式不正确。

示例

在以下示例中,新 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.AppendChild("<pages>100</pages>");

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.AppendChild("<pages>100</pages>")

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>

注解

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

若要创建新元素节点,请在 XML 字符串参数中包含所有 XML 语法。 新 book 节点的字符串为 AppendChild("<book/>")。 用于将文本“book”追加到当前节点的文本节点的字符串为 AppendChild("book")。 如果 XML 字符串包含多个节点,则添加所有节点。

以下是使用 AppendChild 该方法时要考虑的重要说明。

适用于

AppendChild(XmlReader)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

使用指定的对象的 XML 内容 XmlReader 在当前节点的子节点列表末尾创建新的子节点。

public:
 virtual void AppendChild(System::Xml::XmlReader ^ newChild);
public virtual void AppendChild(System.Xml.XmlReader newChild);
abstract member AppendChild : System.Xml.XmlReader -> unit
override this.AppendChild : System.Xml.XmlReader -> unit
Public Overridable Sub AppendChild (newChild As XmlReader)

参数

newChild
XmlReader

XmlReader位于新子节点的 XML 数据上的对象。

例外

对象 XmlReader 处于错误状态或已关闭。

对象 XmlReader 参数为 null.

定位到的当前节点 XPathNavigator 不是根节点或元素节点。

对象参数的 XmlReader XML 内容格式不正确。

示例

在以下示例中,使用指定的对象将新的pages子元素追加到文件中book第一个contosoBooks.xml元素的子元素XmlReader列表中。 指定 http://www.contoso.com/books 命名空间,以便使用与 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");

XmlReader pages = XmlReader.Create(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));

navigator.AppendChild(pages);

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")

Dim pages As XmlReader = XmlReader.Create(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))

navigator.AppendChild(pages)

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>

注解

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

以下是使用 AppendChild 该方法时要考虑的重要说明。

适用于

AppendChild(XPathNavigator)

Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs
Source:
XPathNavigator.cs

使用指定节点中的节点 XPathNavigator 在当前节点的子节点列表末尾创建新的子节点。

public:
 virtual void AppendChild(System::Xml::XPath::XPathNavigator ^ newChild);
public virtual void AppendChild(System.Xml.XPath.XPathNavigator newChild);
abstract member AppendChild : System.Xml.XPath.XPathNavigator -> unit
override this.AppendChild : System.Xml.XPath.XPathNavigator -> unit
Public Overridable Sub AppendChild (newChild As XPathNavigator)

参数

newChild
XPathNavigator

XPathNavigator放置在要添加为新子节点的节点上的对象。

例外

对象 XPathNavigator 参数为 null.

定位到的当前节点 XPathNavigator 不是根节点或元素节点。

示例

在下面的示例中,使用指定中包含的节点pages,将新的book子元素追加到文件中第一个contosoBooks.xml元素的子元素XPathNavigator列表中。 指定 http://www.contoso.com/books 命名空间,以便使用与 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");

XmlDocument childNodes = new XmlDocument();
childNodes.Load(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator childNodesNavigator = childNodes.CreateNavigator();

if(childNodesNavigator.MoveToChild("pages", "http://www.contoso.com/books"))
{
    navigator.AppendChild(childNodesNavigator);
}

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")

Dim childNodes As XmlDocument = New XmlDocument()

childNodes.Load(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))
Dim childNodesNavigator As XPathNavigator = childNodes.CreateNavigator()

If childNodesNavigator.MoveToChild("pages", "http://www.contoso.com/books") Then
    navigator.AppendChild(childNodesNavigator)
End If

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>

注解

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

以下是使用 AppendChild 该方法时要考虑的重要说明。

适用于