XmlNode.InsertBefore(XmlNode, XmlNode) 메서드

정의

지정된 노드를 지정된 참조 노드 바로 앞에 삽입합니다.

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

매개 변수

newChild
XmlNode

삽입할 노드입니다.

refChild
XmlNode

참조 노드입니다. newChild는 이 노드 앞에 삽입됩니다.

반환

XmlNode

삽입할 노드입니다.

예외

현재 노드가 newChild 노드 형식의 자식 노드를 허용하지 않는 형식인 경우

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

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

refChild가 이 노드의 자식이 아닌 경우

이 노드가 읽기 전용인 경우

예제

다음 예제에서는 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->InsertBefore( elem, root->FirstChild );
   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.InsertBefore(elem, root.FirstChild);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Option Strict
Option Explicit

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.InsertBefore(elem, root.FirstChild)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

설명

null경우 refChild 자식 노드 목록의 끝에 삽입 newChild 합니다. 의 모든 내용 newChild 이 같은 순서 refChild로 삽입됩니다. 트리에 newChild 이미 있는 경우 원래 위치에서 제거되고 대상 위치에 추가됩니다. 노드 삽입에 대한 자세한 내용은 XML 문서에 노드 삽입을 참조하세요.

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

상속자 참고

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

적용 대상

추가 정보