Reputation: 1077
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?
Upvotes: 0
Views: 6183
Reputation: 10357
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);
// new code
thumbEl.InnerText = textBox2.Text.Trim();
Upvotes: 3
Reputation: 13599
you are appending it to wrong item change this
itemEl.AppendChild(xmlThumb);
to this
thumbEl.AppendChild(xmlThumb);
Upvotes: 1