XmlNode.AppendChild(XmlNode) 메서드

정의

지정된 노드를 이 노드의 자식 노드 목록 끝에 추가합니다.

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

매개 변수

newChild
XmlNode

추가할 노드입니다. 지정된 위치로 이동하는, 추가할 노드의 모든 콘텐츠입니다.

반환

XmlNode

추가한 노드입니다.

예외

이 노드가 newChild 노드 형식의 자식 노드를 허용하지 않는 형식을 가지는 경우

newChild가 이 노드의 상위 노드일 경우

이 노드를 만든 문서가 아닌 다른 문서에서 newChild를 만든 경우

이 노드가 읽기 전용인 경우

예제

다음 예제에서는 XML 문서에 새 노드를 추가합니다.

#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 );
}
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);
  }
}
Option Explicit
Option Strict

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
End Class

출력:

Display the modified XML...  
<?xml version="1.0" encoding="IBM437"?>  
<book genre="novel" ISBN="1-861001-57-5">  
  <title>Pride And Prejudice</title>  
  <price>19.95</price>  

설명

트리에 newChild 이미 있는 경우 원래 위치에서 제거되고 대상 위치에 추가됩니다. 노드 삽입에 대한 자세한 내용은 XML 문서에 노드 삽입을 참조하세요.

삽입되는 노드가 다른 문서에서 만들어진 경우 노드를 현재 문서로 가져오는 데 사용할 XmlDocument.ImportNode 수 있습니다. 가져온 노드를 현재 문서에 삽입할 수 있습니다.

상속자 참고

파생 클래스에서 재정의할 AppendChild 때 이벤트가 올바르게 발생하려면 기본 클래스의 메서드를 AppendChild 호출해야 합니다.

적용 대상

추가 정보