XmlNode.PrependChild(XmlNode) Método

Definición

Agrega el nodo especificado al principio de la lista de nodos secundarios de este nodo.

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

Parámetros

newChild
XmlNode

Nodo que se va a agregar. Todo el contenido del nodo que se va a agregar se pasa a la ubicación especificada.

Devoluciones

XmlNode

Nodo agregado.

Excepciones

Este nodo 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.

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

Comentarios

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.

Este método es una extensión de Microsoft al Modelo de objetos de documento (DOM).

Notas a los desarrolladores de herederos

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

Se aplica a

Consulte también