XmlNode.InsertBefore(XmlNode, XmlNode) Método

Definición

Inserta el nodo especificado inmediatamente antes del nodo de referencia igualmente especificado.

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

Parámetros

newChild
XmlNode

Nodo que se va a insertar.

refChild
XmlNode

Nodo de referencia. newChild se coloca delante de este nodo.

Devoluciones

XmlNode

Nodo que se va a insertar.

Excepciones

El nodo actual es de un tipo que no permite nodos secundarios del tipo del nodo newChild.

newChild es un nodo antecesor de este nodo.

newChild se creó a partir de un documento diferente del que creó este nodo.

refChild no es un nodo secundario de este nodo.

Este nodo es de sólo lectura.

Ejemplos

En el ejemplo siguiente se agrega un nuevo nodo al documento 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

Comentarios

Si refChild es null, inserte newChild al final de la lista de nodos secundarios. Todo el contenido de newChild se inserta, en el mismo orden, antes refChildde . Si el ya newChild está en el árbol, se quita de su posición original y se agrega a su posición de destino. Para obtener más información sobre cómo insertar nodos, vea Insertar nodos en un documento XML.

Si el nodo que se va a insertar se creó a partir de otro documento, puede usar XmlDocument.ImportNode para importar el nodo al documento actual. A continuación, el nodo importado se puede insertar en el documento actual.

Notas a los desarrolladores de herederos

Al invalidar InsertBefore en una clase derivada, para que los eventos se generen correctamente, debe llamar al InsertBefore método de la clase base.

Se aplica a

Consulte también