i need to add a new item. this new item will be added as child. but it appears that it combined all the data input in only one child item. below is my code.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\Users\\HDAdmin\\Documents\\SliceEngine\\SliceEngine\\bin\\Debug\\saya.xml");
XmlElement contentElement = xmlDoc.CreateElement("Name");
XmlElement itemEl = xmlDoc.CreateElement("item");
XmlText xmlText = xmlDoc.CreateTextNode(textBox1.Text.Trim());
itemEl.AppendChild(xmlText);
contentElement.AppendChild(itemEl);
xmlDoc.DocumentElement.AppendChild(contentElement);
XmlElement thumbEl = xmlDoc.CreateElement("thumb");
XmlText xmlThumb = xmlDoc.CreateTextNode(textBox2.Text.Trim());
itemEl.AppendChild(xmlThumb);
contentElement.AppendChild(thumbEl);
xmlDoc.DocumentElement.AppendChild(contentElement);
xmlDoc.Save("C:\\Users\\HDAdmin\\Documents\\SliceEngine\\SliceEngine\\bin\\Debug\\saya.xml");
The output appears
<Name>
<item>werasd</item>
<thumb/> </Name>
but the output should appears
<Name>
<item>wer</item>
<thumb>asd<thumb/>
what should i do?
Use
XmlElement.InnerText. It gets or sets the concatenated values of the node. So that your code should be like this:XmlText xmlThumb = xmlDoc.CreateTextNode(textBox2.Text.Trim());itemEl.AppendChild(xmlThumb);