1 Replies - 657 Views - Last Post: 13 January 2013 - 03:14 AM Rate Topic: ***** 1 Votes

#1 articuno   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-May 12

Problem with XML DOM, writing in XML document

Posted 13 January 2013 - 02:27 AM

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with XML DOM, writing in XML document

#2 articuno   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-May 12

Re: Problem with XML DOM, writing in XML document

Posted 13 January 2013 - 03:14 AM

I've found a bug! In the first case i have
XmlElement novDatum = newDoc.CreateElement("datum");



and in the second case i have
XmlElement novNabave = doc.CreateElement("nabave");



First i use newDoc, and the second i use doc.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1