Click here to Skip to main content
11,705,610 members (55,645 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML DOM
Hi!
I have a problem with writing to an XML file. When i run my program, the c# give me an exception: "The node to be inserted is from a different document context.". I looked a bit on the exception and I found the answers , that this is happening when we write from one document to another. But i dont write from one to another, but I write from the object to xml file. This is the code where the program shows me exception:

private XmlDocument novDokument = new XmlDocument();
 
public void zapisiArtikleKiSoNaZalogiVnewDoc(string vhodna, string izhodna)
{
    int i = 1;
 
    newDoc.AppendChild(newDoc.CreateXmlDeclaration(new Version(1, 0).ToString(), Encoding.UTF8.BodyName, string.Empty));
    XmlElement novSeznamArtikel = newDoc.CreateElement("seznamArtiklov");
 
    foreach (Artikel a in seznamPrebranihArtiklov)
    {
        if (Convert.ToInt16(a.zalogaA) > 0)
        {
            XmlElement novArtikel = newDoc.CreateElement("artikel");
            novArtikel.SetAttribute("id", i.ToString());
 
            XmlElement novNaziv = newDoc.CreateElement("naziv");
            novNaziv.InnerText = a.nazivA;
            novArtikel.AppendChild(novNaziv);
 
            XmlElement novaCena = newDoc.CreateElement("cena");
            novaCena.InnerText = a.cenaA;
            novArtikel.AppendChild(novaCena);
 

            XmlElement novDatum = newDoc.CreateElement("datum");
            novArtikel.AppendChild(novDatum);
 
            XmlElement novNabave = doc.CreateElement("nabave");
            novNabave.InnerText = a.datumNabaveA;
            novDatum.AppendChild(novNabave); //HERE RAISES AN EXCEPTION

            novSeznamArtikel.AppendChild(novArtikel);
            i++;
        }
    }
 
    newDoc.AppendChild(novSeznamArtikel);
 
    XmlTextWriter tw = new XmlTextWriter(izhodna, null);
    tw.Formatting = Formatting.Indented;
    newDoc.WriteContentTo(tw);
 
    tw.Close();
}

And XML document should look like this:
<?xml version="1.0" encoding="utf-8" ?>
<seznamArtiklov>
  <artikel id="1">
    <naziv>Kruh</naziv>
    <cena>3</cena>
    <datum>
      <nabave>03.09.2012</nabave>
    </datum>
  </artikel>
</seznamArtiklov

If anyone knows the solution to my problem is very welcome. To answer thank you in advance!
Posted 12-Jan-13 22:08pm
vezo11773
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Try changing this:
XmlElement novDatum = newDoc.CreateElement("datum");
novArtikel.AppendChild(novDatum);
 
XmlElement novNabave = doc.CreateElement("nabave");
novNabave.InnerText = a.datumNabaveA;
novDatum.AppendChild(novNabave); //HERE RAISES AN EXCEPTION
into this:
XmlElement novDatum = newDoc.CreateElement("datum");
 
XmlElement novNabave = doc.CreateElement("nabave");
novNabave.InnerText = a.datumNabaveA;
novDatum.AppendChild(newDoc.ImportNode(novNabave, true)); // first import the node into the document, and then append novNabave to novDatum before you append novDatum to novArtikel

novArtikel.AppendChild(novDatum);

Hope this helps.
  Permalink  
v2
Comments
vezo11 at 13-Jan-13 4:59am
   
Thank you very much! :)

I assumed that elements do not nest correctly or in a way, first add the file and then append.

Again thanks for the reply!
ProgramFOX at 13-Jan-13 5:02am
   
You're welcome!
vezo11 at 13-Jan-13 5:09am
   
And if you look this:
XmlElement novDatum = newDoc.CreateElement("datum");
novArtikel.AppendChild(novDatum);
and this:
XmlElement novNabave = doc.CreateElement("nabave");
novNabave.InnerText = a.datumNabaveA;
novDatum.AppendChild(novNabave);

You see the difference.. First i use newDoc, in second case i use doc. My mistake, because I changed the code, when I repaired one another mistake and I forgot to change it back.

But of course, both solutions works! :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
i have tried, but the same effect. Exception signals on the same line:
novDatum.AppendChild(novNabave);

Thanks for the reply.
  Permalink  
Comments
ProgramFOX at 13-Jan-13 4:53am
   
I updated my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 229
1 Sergey Alexandrovich Kryukov 100
2 Inimicos 60
3 ProgramFOX 50
4 Richard MacCutchan 45
0 OriginalGriff 9,053
1 Sergey Alexandrovich Kryukov 8,347
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100