I would like to create an XML file like this:
<?xml version="1.0" encoding="UTF-8"?>
<text>
<languages>
<language id =1>English</language>
<language1 id=2>Slovenian</language1>
</languages>
<strings>
<line id=1>
<string lid=1>camera</string>
<string lid=2>kamera</string1>
</line>
<line id=2>
<string lid=1>lens</string>
<string1 lid=2>leka</string1>
</line>
</strings>
</text>
I am trying with the following code in C# (Windows Forms Application):
XmlDocument xmldoc = new XmlDocument();
XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", null, null);
xmldoc.AppendChild(xmldec);
XmlElement root = xmldoc.CreateElement("text");
xmldoc.AppendChild(root);
XmlElement lang = xmldoc.CreateElement("languages");
XmlElement languages = xmldoc.CreateElement("language");
languages.SetAttribute("id", "1");
languages.InnerText = "English";
lang.PrependChild(languages);
languages.SetAttribute("id", "2");
languages.InnerText = "Slovenian";
lang.PrependChild(languages);
XmlElement lines = xmldoc.CreateElement("strings");
XmlElement line = xmldoc.CreateElement("line");
XmlElement lineinner = xmldoc.CreateElement("string");
line.SetAttribute("id", "1");
lineinner.SetAttribute("lid", "1");
lineinner.InnerText = "some english text";
line.AppendChild(lineinner);
line.SetAttribute("id", "1");
lineinner.SetAttribute("lid", "2");
lineinner.InnerText = "some slovenian text";
line.AppendChild(lineinner);
lines.AppendChild(line);
root.AppendChild(lang);
root.AppendChild(lines);
Metodi.SerializeXMLToXML(xmldoc);
And i end up with the following result:
<?xml version="1.0" encoding="UTF-8"?>
<text>
<languages>
<language id="2">Slovenian</language>
</languages>
<strings>
<line id="2">
<string lid="2">some slovenian text</string>
</line>
</strings>
</text>
After every next AppendChild() replaces the previous is there any way to achieve this??
Re-initialize it every time is one solution:
Nodes can only have one parent, hence your problem.
A nicer way to resolve this, though, might be to use XML literals.It has come to my attention that C# does not support XML literals. This doesn’t change the fact that you can write this in a nicer way using XML serialization or loops and such.