XmlNode.AppendChild 方法

将指定的节点添加到该节点的子节点列表的末尾。

**命名空间:**System.Xml
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
Public Overridable Function AppendChild ( _
    newChild As XmlNode _
) As XmlNode
用法
Dim instance As XmlNode
Dim newChild As XmlNode
Dim returnValue As XmlNode

returnValue = instance.AppendChild(newChild)
public virtual XmlNode AppendChild (
    XmlNode newChild
)
public:
virtual XmlNode^ AppendChild (
    XmlNode^ newChild
)
public XmlNode AppendChild (
    XmlNode newChild
)
public function AppendChild (
    newChild : XmlNode
) : XmlNode

参数

  • newChild
    要添加的节点。如果它是一个 XmlDocumentFragment,则将该文档片段的全部内容移动到该节点的子列表中。

返回值

添加的节点。

异常

异常类型 条件

InvalidOperationException

此节点的类型不允许 newChild 节点类型的子节点。

newChild 是此节点的上级节点。

ArgumentException

newChild 是从不同于创建此节点的文档创建的。

该节点是只读的。

备注

如果 newChild 已经在树中,则先将其移除。

如果插入的节点是从另一个文档创建的,则可以使用 XmlDocument.ImportNode 将该节点导入到当前文档中。随后,导入的节点即可插入到当前文档中。

给继承者的说明 当在派生类中重写 AppendChild 时,若要使事件正确引发,必须调用基类的 AppendChild 方法。

示例

下面的示例将一个新节点添加到 XML 文档。

Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "</book>")
        
        Dim root As XmlNode = doc.DocumentElement
        
        'Create a new node.
        Dim elem As XmlElement = doc.CreateElement("price")
        elem.InnerText = "19.95"
        
        'Add the node to the document.
        root.AppendChild(elem)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub 'Main 
End Class 'Sample
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //Create a new node.
    XmlElement elem = doc.CreateElement("price");
    elem.InnerText="19.95";

    //Add the node to the document.
    root.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

  }
}
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' ISBN='1-861001-57-5'>"
   "<title>Pride And Prejudice</title>"
   "</book>" );
   XmlNode^ root = doc->DocumentElement;
   
   //Create a new node.
   XmlElement^ elem = doc->CreateElement( "price" );
   elem->InnerText = "19.95";
   
   //Add the node to the document.
   root->AppendChild( elem );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"
            + "<title>Pride And Prejudice</title>"
            + "</book>");
        XmlNode root = doc.get_DocumentElement();

        //Create a new node.
        XmlElement elem = doc.CreateElement("price");
        elem.set_InnerText("19.95");

        //Add the node to the document.
        root.AppendChild(elem);

        Console.WriteLine("Display the modified XML...");
        doc.Save(Console.get_Out());
    } //main 
} //Sample

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

XmlNode 类
XmlNode 成员
System.Xml 命名空间
XmlDocument.ImportNode