Share via


XPathNavigator.PrependChild Méthode

Définition

Crée un nœud enfant au début de la liste de nœuds enfants du nœud actuel.

Surcharges

PrependChild()

Retourne un objet XmlWriter permettant de créer un nœud enfant au début de la liste de nœuds enfants du nœud actuel.

PrependChild(String)

Crée un nœud enfant au début de la liste de nœuds enfants du nœud actuel à l'aide de la chaîne XML spécifiée.

PrependChild(XmlReader)

Crée un nœud enfant au début de la liste des nœuds enfants du nœud actuel en utilisant le contenu XML de l’objet XmlReader spécifié.

PrependChild(XPathNavigator)

Crée un nœud enfant au début de la liste de nœuds enfants du nœud actuel à l’aide des nœuds de l’objet XPathNavigator spécifié.

PrependChild()

Retourne un objet XmlWriter permettant de créer un nœud enfant au début de la liste de nœuds enfants du nœud actuel.

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

Retours

XmlWriter

Objet XmlWriter permettant de créer un nœud enfant au début de la liste de nœuds enfants du nœud actuel.

Exceptions

Le nœud actuel sur lequel XPathNavigator est positionné ne permet pas l'ajout d'un nouveau nœud enfant en début de liste.

Le XPathNavigator ne prend pas en charge la modification.

Exemples

Dans l’exemple suivant, un nouvel pages élément enfant est ajouté au début de la liste des éléments enfants du premier book élément du contosoBooks.xml fichier à l’aide de l’objet XmlWriter retourné par la PrependChild méthode.

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");

XmlWriter^ pages = navigator->PrependChild();
pages->WriteElementString("pages", "100");
pages->Close();

Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");

XmlWriter pages = navigator.PrependChild();
pages.WriteElementString("pages", "100");
pages.Close();

Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")

Dim pages As XmlWriter = navigator.PrependChild()
pages.WriteElementString("pages", "100")
pages.Close()

Console.WriteLine(navigator.OuterXml)

L'exemple prend le fichier contosoBooks.xml comme entrée.

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

Remarques

Un nœud enfant prépending ajoute le nouveau nœud au début de la liste des nœuds enfants pour le nœud actuel. Par exemple, quand trois nœuds enfants existent pour un élément, le nœud ajouté devient le premier nœud enfant. Si aucun nœud enfant n’existe, un nouveau nœud enfant est créé.

Voici quelques remarques importantes à prendre en compte lors de l’utilisation de la PrependChild méthode.

S’applique à

PrependChild(String)

Crée un nœud enfant au début de la liste de nœuds enfants du nœud actuel à l'aide de la chaîne XML spécifiée.

public:
 virtual void PrependChild(System::String ^ newChild);
public virtual void PrependChild (string newChild);
abstract member PrependChild : string -> unit
override this.PrependChild : string -> unit
Public Overridable Sub PrependChild (newChild As String)

Paramètres

newChild
String

Chaîne de données XML pour le nouveau nœud enfant.

Exceptions

Le paramètre de chaîne XML est null.

Le nœud actuel du XPathNavigator est positionné ou n’autorise pas le placement d’un nouveau nœud enfant en début de liste.

Le XPathNavigator ne prend pas en charge la modification.

Le paramètre de chaîne XML n’est pas mis en forme correctement.

Exemples

Dans l’exemple suivant, un nouvel pages élément enfant est ajouté au début de la liste des éléments enfants du premier book élément du contosoBooks.xml fichier.

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");

navigator->PrependChild("<pages>100</pages>");

Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");

navigator.PrependChild("<pages>100</pages>");

Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")

navigator.PrependChild("<pages>100</pages>")

Console.WriteLine(navigator.OuterXml)

L'exemple prend le fichier contosoBooks.xml comme entrée.

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

Remarques

Un nœud enfant prépending ajoute le nouveau nœud au début de la liste des nœuds enfants pour le nœud actuel. Par exemple, quand trois nœuds enfants existent pour un élément, le nœud ajouté devient le premier nœud enfant. Si aucun nœud enfant n’existe, un nouveau nœud enfant est créé.

Pour créer un nœud d’élément, incluez toute la syntaxe XML dans le paramètre de chaîne XML. La chaîne d’un nouveau book nœud est PrependChild("<book/>"). La chaîne d’ajout du texte « book » au nœud de texte du nœud actuel est PrependChild("book"). Si la chaîne XML contient plusieurs nœuds, tous les nœuds sont ajoutés.

Voici quelques remarques importantes à prendre en compte lors de l’utilisation de la PrependChild méthode.

S’applique à

PrependChild(XmlReader)

Crée un nœud enfant au début de la liste des nœuds enfants du nœud actuel en utilisant le contenu XML de l’objet XmlReader spécifié.

public:
 virtual void PrependChild(System::Xml::XmlReader ^ newChild);
public virtual void PrependChild (System.Xml.XmlReader newChild);
abstract member PrependChild : System.Xml.XmlReader -> unit
override this.PrependChild : System.Xml.XmlReader -> unit
Public Overridable Sub PrependChild (newChild As XmlReader)

Paramètres

newChild
XmlReader

Objet XmlReader positionné sur les données XML pour le nouveau nœud enfant.

Exceptions

L'objet XmlReader est dans un état d'erreur ou est fermé.

Le paramètre de l’objet XmlReader est null.

Le nœud actuel du XPathNavigator est positionné ou n’autorise pas le placement d’un nouveau nœud enfant en début de liste.

Le XPathNavigator ne prend pas en charge la modification.

Le contenu XML du paramètre de l’objet XmlReader n’est pas mis en forme correctement.

Exemples

Dans l’exemple suivant, un nouvel pages élément enfant est ajouté au début de la liste des éléments enfants du premier book élément du contosoBooks.xml fichier à l’aide de l’objet XmlReader spécifié. L’espace http://www.contoso.com/books de noms est spécifié afin que le nouvel élément enfant soit ajouté à l’aide du même espace de noms que le document XML.

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");

XmlReader^ pages = XmlReader::Create(gcnew StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));

navigator->PrependChild(pages);

Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");

XmlReader pages = XmlReader.Create(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));

navigator.PrependChild(pages);

Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")

Dim pages As XmlReader = XmlReader.Create(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))

navigator.PrependChild(pages)

Console.WriteLine(navigator.OuterXml)

L'exemple prend le fichier contosoBooks.xml comme entrée.

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

Remarques

Un nœud enfant prépending ajoute le nouveau nœud au début de la liste des nœuds enfants pour le nœud actuel. Par exemple, quand trois nœuds enfants existent pour un élément, le nœud ajouté devient le premier nœud enfant. Si aucun nœud enfant n’existe, un nouveau nœud enfant est créé.

Voici quelques remarques importantes à prendre en compte lors de l’utilisation de la PrependChild méthode.

  • Si l’objet XmlReader est positionné sur une séquence de nœuds XML, tous les nœuds de la séquence sont ajoutés.

  • La PrependChild méthode est valide uniquement lorsque l’élément XPathNavigator est positionné sur un nœud d’élément.

  • La PrependChild méthode n’affecte pas la position du XPathNavigator.

S’applique à

PrependChild(XPathNavigator)

Crée un nœud enfant au début de la liste de nœuds enfants du nœud actuel à l’aide des nœuds de l’objet XPathNavigator spécifié.

public:
 virtual void PrependChild(System::Xml::XPath::XPathNavigator ^ newChild);
public virtual void PrependChild (System.Xml.XPath.XPathNavigator newChild);
abstract member PrependChild : System.Xml.XPath.XPathNavigator -> unit
override this.PrependChild : System.Xml.XPath.XPathNavigator -> unit
Public Overridable Sub PrependChild (newChild As XPathNavigator)

Paramètres

newChild
XPathNavigator

Objet XPathNavigator positionné sur le nœud à ajouter en tant que nouveau nœud enfant.

Exceptions

Le paramètre de l’objet XPathNavigator est null.

Le nœud actuel du XPathNavigator est positionné ou n’autorise pas le placement d’un nouveau nœud enfant en début de liste.

Le XPathNavigator ne prend pas en charge la modification.

Exemples

Dans l’exemple suivant, un nouvel pages élément enfant est prépendé au début de la liste des éléments enfants du premier élément du contosoBooks.xml fichier book à l’aide du nœud contenu dans l’objet XPathNavigator spécifié. L’espace http://www.contoso.com/books de noms est spécifié afin que le nouvel élément enfant soit prépendé à l’aide du même espace de noms que le document XML.

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");

XmlDocument^ childNodes = gcnew XmlDocument();
childNodes->Load(gcnew StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator^ childNodesNavigator = childNodes->CreateNavigator();

navigator->PrependChild(childNodesNavigator);

Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");

XmlDocument childNodes = new XmlDocument();
childNodes.Load(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator childNodesNavigator = childNodes.CreateNavigator();

navigator.PrependChild(childNodesNavigator);

Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")

Dim childNodes As XmlDocument = New XmlDocument()
childNodes.Load(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))
Dim childNodesNavigator As XPathNavigator = childNodes.CreateNavigator()

navigator.PrependChild(childNodesNavigator)

Console.WriteLine(navigator.OuterXml)

L'exemple prend le fichier contosoBooks.xml comme entrée.

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

Remarques

Un nœud enfant prépending ajoute le nouveau nœud au début de la liste des nœuds enfants pour le nœud actuel. Par exemple, quand trois nœuds enfants existent pour un élément, le nœud ajouté devient le premier nœud enfant. Si aucun nœud enfant n’existe, un nouveau nœud enfant est créé.

Voici quelques remarques importantes à prendre en compte lors de l’utilisation de la PrependChild méthode.

S’applique à