2010-11-09 11 views
9


Stavo cercando di inserire un nodo xml prima di un altro xmlnode e ho ottenuto un'eccezione dicendo "Il nodo di riferimento non è figlio di questo nodo".Si è verificata un'eccezione durante il tentativo di utilizzare "InsertBefore" di XmlDocument in C#

Questo è il mio xml iniziale:

<?xml version="1.0" encoding="utf-8" ?> 
<Details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <sampleData> 
    <otherNodes></otherNodes> 
    </sampleData> 
</Details> 

ho voluto inserire in seguito dati XML (B: dataTobeInserted1, B: dataTobeInserted2 e B: dataTobeInserted3) come un figlio di dettagli ma prima sampleData.

Details1.xml

<?xml version="1.0" encoding="utf-8" ?> 
<DataInserted1 xmlns:b="http://example.com/data"> 
    <b:dataTobeInserted1> 
    <b:otherDetails1></b:otherDetails1> 
    </b:dataTobeInserted1> 
</DataInserted1> 

Details2.xml

<?xml version="1.0" encoding="utf-8" ?> 
<DataInserted2 xmlns:b="http://example.com/data"> 
    <b:dataTobeInserted2> 
    <b:otherDetails2></b:otherDetails2> 
    </b:dataTobeInserted2> 
</DataInserted2> 

Details3.xml

<?xml version="1.0" encoding="utf-8" ?> 
<DataInserted3 xmlns:b="http://example.com/data"> 
    <b:dataTobeInserted3> 
    <b:otherDetails3></b:otherDetails3> 
    </b:dataTobeInserted3> 
</DataInserted3> 

Voglio che la mia uscita da

<?xml version="1.0" encoding="utf-8" ?> 
<Details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:b="http://example.com/data"> 
    <b:dataTobeInserted1> 
    <b:otherDetails1></b:otherDetails1> 
    </b:dataTobeInserted1> 
    <b:dataTobeInserted2> 
    <b:otherDetails2></b:otherDetails2> 
    </b:dataTobeInserted2> 
    <b:dataTobeInserted3> 
    <b:otherDetails3></b:otherDetails3> 
    </b:dataTobeInserted3> 
    <sampleData> 
    <otherNodes></otherNodes> 
    </sampleData> 
</Details> 

Questo è quello che ho fatto per ottenere l'output desiderato.

XmlDocument xmldoc = new XmlDocument(); 
    xmldoc.Load(@"..\..\initial-Doc.xml"); 

    xmldoc.DocumentElement.SetAttribute("xmlns:b", "http://example.com/data"); 


     XmlDocument detail1 = new XmlDocument(); 
     detail1.Load(@"..\..\DataToBeInserted1.xml"); 
     XmlNode detail1Node = xmldoc.ImportNode(detail1.DocumentElement, true); 

     XmlDocument detail2 = new XmlDocument(); 
     detail2.Load(@"..\..\DataToBeInserted2.xml"); 
     XmlNode detail2Node = xmldoc.ImportNode(detail2.DocumentElement, true); 

     XmlDocument detail3 = new XmlDocument(); 
     detail3.Load(@"..\..\DataToBeInserted3.xml"); 
     XmlNode detail3Node = xmldoc.ImportNode(detail3.DocumentElement, true); 

    xmldoc.InsertBefore(detail1Node, xmldoc.DocumentElement.FirstChild); 
    xmldoc.InsertBefore(detail2Node, xmldoc.DocumentElement.FirstChild); 
    xmldoc.InsertBefore(detail3Node, xmldoc.DocumentElement.FirstChild); 

    xmldoc.Save(@"..\..\initial-Doc-new.xml"); 

Il nuovo spazio dei nomi sta causando il problema? Per favore, dimmi dove ho sbagliato.

Grazie Alex

risposta

21

Mi sembra che il problema sia esattamente quello che dice l'eccezione: "Il nodo di riferimento non è figlio di questo nodo". L'unica parte difficile è capire a quali nodi si riferiscono. :-)

Here "questo nodo" si intende il XmlNode cui InsertBefore() metodo che si sta chiamando, e "il riferimento del nodo" significa il secondo argomento di InsertBefore(). Quindi, nella riga seguente:

xmldoc.InsertBefore(detail1Node, xmldoc.DocumentElement.FirstChild); 

E 'dire che xmldoc.DocumentElement.FirstChild non è un figlio di xmldoc.

Quale è vero. xmldoc.DocumentElement.FirstChild è un figlio di xmldoc.DocumentElement.

Quindi, se si cambia quella linea al seguente, il problema dovrebbe essere risolto:

xmldoc.DocumentElement.InsertBefore(detail1Node, xmldoc.DocumentElement.FirstChild); 
+1

grazie amico ha funzionato, grazie mille per la bella spiegazione per il mio problema. Apprezzo che :) – wizzardz

+0

@ Alex: felice di essere al servizio! – LarsH

+0

Questo risolve anche il mio problema. Vorrei che questo documento XML fosse un po 'più chiaro –

1

È necessario fare una copia del nodo, perché non è possibile aggiungere un nodo XML da un documento all'altro.

io penso copiando il nodo utilizzando il metodo CloneNode() dovrebbe essere sufficiente, ma se non funziona si potrebbe creare un nuovo nodo dai nodi InnerXml o OuterXml proprietà.

+0

Se si dà un'occhiata in XML uscita si può vedere che c'è un nuovo spazio dei nomi aggiunto ad esso. è questo che causa un problema? – wizzardz

0

è necessario utilizzare il metodo ImportNode della classe XmlDocument:

XmlNode importedDetailsNode = xmldoc.ImportNode(detail3.DocumentElement, true); 
xmldoc.InsertBefore(importedDetailsNode , xmldoc.DocumentElement.FirstChild); 

vedere here per la documentazione MSDN.

+0

Fatto, ma funziona ancora – wizzardz

Problemi correlati