XML DOM的问题,用XML文档编写 [英] Problem with XML DOM, writing in XML document
问题描述
嗨!
我在写入XML文件时遇到问题。当我运行我的程序时,c#给了我一个例外:要插入的节点来自不同的文档上下文。我看了一下异常,我找到了答案,当我们从一个文档写到另一个文档时,就会发生这种情况。但我不会从一个写到另一个,但我从对象写入xml文件。这是程序向我显示异常的代码:
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();
}
XML文档应如下所示:
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!
推荐答案
尝试更改:
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.
i已尝试,但效果相同。同一行上的异常信号:
Hi,
i have tried, but the same effect. Exception signals on the same line:
novDatum.AppendChild(novNabave);
感谢您的回复。
Thanks for the reply.
这篇关于XML DOM的问题,用XML文档编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!