I am adding a node to the XML file, but I need it to be properly formatted. Could you assist with it?
String newFile = System.IO.Path.GetFileName(textBox1.Text);
//file name
string filename = @"palette.xml";
XmlDocument doc = new XmlDocument();
doc.Load(filename);
//create node and add value
XmlNode node = doc.CreateNode(XmlNodeType.Element, "item", null);
//create title node
XmlNode nodeTitle = doc.CreateElement("name");
//add value for it
nodeTitle.InnerText = @"<![CDATA["+newFile+"]]>";
//create Url node
XmlNode nodeUrl = doc.CreateElement("imgfile");
nodeUrl.InnerText = newFile;
//add to parent node
node.AppendChild(nodeTitle);
node.AppendChild(nodeUrl);
//add to elements collection
doc.DocumentElement.AppendChild(node);
//save back
doc.Save(filename);
The XML should be looking like that:
<item>
<name><![CDATA[panda.gif]]></name>
<imgfile>panda.gif</imgfile>
</item>
but it look like that:
<item>
<name><![CDATA[panda.gif]]></name>
<imgfile>panda.gif</imgfile>
</item>
There is a method you can use to wrap cdata it is.
It XMLDocument.CreateCDataSection returns XmlCDataSection object which you can append to your node and it will wrap your file in CDATA.
Check this out for more information: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx