XPathNavigator.PrependChild Metodo

Definizione

Crea un nuovo nodo figlio all'inizio dell'elenco dei nodi figlio del nodo corrente.

Overload

PrependChild()

Restituisce un oggetto XmlWriter utilizzato per creare un nuovo nodo figlio all'inizio dell'elenco dei nodi figlio del nodo corrente.

PrependChild(String)

Crea un nuovo nodo figlio all'inizio dell'elenco di nodi figlio del nodo corrente usando la stringa XML specificata.

PrependChild(XmlReader)

Crea un nuovo nodo figlio all'inizio dell'elenco di nodi figlio del nodo corrente usando il contenuto XML dell'oggetto XmlReader specificato.

PrependChild(XPathNavigator)

Crea un nuovo nodo figlio all'inizio dell'elenco di nodi figlio del nodo corrente usando i nodi dell'oggetto XPathNavigator specificato.

PrependChild()

Restituisce un oggetto XmlWriter utilizzato per creare un nuovo nodo figlio all'inizio dell'elenco dei nodi figlio del nodo corrente.

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

Restituisce

XmlWriter

Oggetto XmlWriter usato per creare un nuovo nodo figlio all'inizio dell'elenco di nodi figlio del nodo corrente.

Eccezioni

Il nodo corrente su cui è posizionato l'oggetto XPathNavigator non consente l'anteposizione di un nuovo nodo figlio.

XPathNavigator non supporta la modifica.

Esempio

Nell'esempio seguente viene anteporto un nuovo pages elemento figlio all'inizio dell'elenco di elementi figlio del primo book elemento del contosoBooks.xml file utilizzando l'oggetto XmlWriter restituito dal PrependChild metodo .

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)

Nell'esempio il file contosoBooks.xml viene considerato come input.

<?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>  

Commenti

In attesa di un nodo figlio, il nuovo nodo viene aggiunto all'inizio dell'elenco di nodi figlio per il nodo corrente. Ad esempio, quando esistono tre nodi figlio per un elemento, il nodo anteporto diventa il primo nodo figlio. Se non esistono nodi figlio, viene creato un nuovo nodo figlio.

Di seguito sono riportate note importanti da considerare quando si usa il PrependChild metodo .

Si applica a

PrependChild(String)

Crea un nuovo nodo figlio all'inizio dell'elenco di nodi figlio del nodo corrente usando la stringa XML specificata.

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)

Parametri

newChild
String

Stringa di dati XML per il nuovo nodo figlio.

Eccezioni

Il parametro di stringa XML è null.

Il nodo corrente su cui è posizionato l'oggetto XPathNavigator non consente di anteporre un nuovo nodo figlio.

XPathNavigator non supporta la modifica.

Il formato del parametro di stringa XML non è corretto.

Esempio

Nell'esempio seguente viene anteporto un nuovo pages elemento figlio all'inizio dell'elenco di elementi figlio del primo book elemento nel contosoBooks.xml file.

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)

Nell'esempio il file contosoBooks.xml viene considerato come input.

<?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>  

Commenti

In attesa di un nodo figlio, il nuovo nodo viene aggiunto all'inizio dell'elenco di nodi figlio per il nodo corrente. Ad esempio, quando esistono tre nodi figlio per un elemento, il nodo anteporto diventa il primo nodo figlio. Se non esistono nodi figlio, viene creato un nuovo nodo figlio.

Per creare un nuovo nodo elemento, includere tutta la sintassi XML nel parametro stringa XML. La stringa per un nuovo book nodo è PrependChild("<book/>"). La stringa per aggiungere il testo "book" al nodo di testo del nodo corrente è PrependChild("book"). Se la stringa XML contiene più nodi, vengono aggiunti tutti i nodi.

Di seguito sono riportate note importanti da considerare quando si usa il PrependChild metodo .

Si applica a

PrependChild(XmlReader)

Crea un nuovo nodo figlio all'inizio dell'elenco di nodi figlio del nodo corrente usando il contenuto XML dell'oggetto XmlReader specificato.

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)

Parametri

newChild
XmlReader

Oggetto XmlReader posizionato in corrispondenza dei dati XML per il nuovo nodo figlio.

Eccezioni

L'oggetto XmlReader si trova in uno stato di errore oppure è chiuso.

Il parametro dell'oggetto XmlReader è null.

Il nodo corrente su cui è posizionato l'oggetto XPathNavigator non consente di anteporre un nuovo nodo figlio.

XPathNavigator non supporta la modifica.

Il formato del contenuto XML del parametro dell'oggetto XmlReader non è corretto.

Esempio

Nell'esempio seguente viene anteporto un nuovo pages elemento figlio all'inizio dell'elenco di elementi figlio del primo book elemento del contosoBooks.xml file utilizzando l'oggetto XmlReader specificato. Lo http://www.contoso.com/books spazio dei nomi viene specificato in modo che il nuovo elemento figlio venga anteporto utilizzando lo stesso spazio dei nomi del documento 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)

Nell'esempio il file contosoBooks.xml viene considerato come input.

<?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>  

Commenti

In attesa di un nodo figlio, il nuovo nodo viene aggiunto all'inizio dell'elenco di nodi figlio per il nodo corrente. Ad esempio, quando esistono tre nodi figlio per un elemento, il nodo anteporto diventa il primo nodo figlio. Se non esistono nodi figlio, viene creato un nuovo nodo figlio.

Di seguito sono riportate note importanti da considerare quando si usa il PrependChild metodo .

Si applica a

PrependChild(XPathNavigator)

Crea un nuovo nodo figlio all'inizio dell'elenco di nodi figlio del nodo corrente usando i nodi dell'oggetto XPathNavigator specificato.

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)

Parametri

newChild
XPathNavigator

Oggetto XPathNavigator posizionato in corrispondenza del nodo da aggiungere come nuovo nodo figlio.

Eccezioni

Il parametro dell'oggetto XPathNavigator è null.

Il nodo corrente su cui è posizionato l'oggetto XPathNavigator non consente di anteporre un nuovo nodo figlio.

XPathNavigator non supporta la modifica.

Esempio

Nell'esempio seguente viene preceduto un nuovo pages elemento figlio all'inizio dell'elenco degli elementi figlio del primo book elemento contosoBooks.xml del file usando il nodo contenuto nell'oggetto XPathNavigator specificato. Lo http://www.contoso.com/books spazio dei nomi viene specificato in modo che il nuovo elemento figlio venga prependato usando lo stesso spazio dei nomi del documento 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)

Nell'esempio il file contosoBooks.xml viene considerato come input.

<?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>  

Commenti

In attesa di un nodo figlio, il nuovo nodo viene aggiunto all'inizio dell'elenco di nodi figlio per il nodo corrente. Ad esempio, quando esistono tre nodi figlio per un elemento, il nodo anteporto diventa il primo nodo figlio. Se non esistono nodi figlio, viene creato un nuovo nodo figlio.

Di seguito sono riportate note importanti da considerare quando si usa il PrependChild metodo .

Si applica a