I want to modify a XML file but the tricky part is that the info I add should have minimal formatting. I have been searching for a while and I can't seem to find an answer. Here's what the XML looks currently (original):
<?xml version="1.0" encoding="utf-8"?>
<doe-jane>
<name>Jane Doe</name>
<contact>North Pole
Tel: (555) 123-4567
jane.doe@abc.com
</contact>
<coveragelist>
<company>Comp1</company>
<company>Comp2</company>
</coveragelist>
</doe-jane>
It has to look like this:
<?xml version="1.0" encoding="utf-8"?>
<doe-jane>
<name>Jane Doe</name>
<contact>North Pole
Tel: (555) 123-4567
jane.doe@abc.com
</contact>
--> // Change needs to happen from here on <--
<coveragelist><company>Comp1</company>
<company>Comp2</company>
</coveragelist>
</doe-jane>
Here's my code so far:
XmlDocument d = new XmlDocument();
//XmlTextWriter wr = new XmlTextWriter(resAnXFile, Encoding.UTF8);
//wr.Formatting = Formatting.None;
d.Load(resAnXFile);
XmlNode t = d.SelectSingleNode("//coveragelist");
t.ParentNode.RemoveChild(t);
// create CoverageList node
XmlNode coverageListNode = d.CreateNode(XmlNodeType.Element, "coveragelist", null);
foreach (var company in dataList)
{
// create company nodes
XmlNode companyNode = d.CreateElement("company");
companyNode.InnerText = company.CompanyName.ToString();
coverageListNode.AppendChild(companyNode);
}
d.DocumentElement.AppendChild(coverageListNode);
d.Save(resAnXFile);
I've tried XMLTextWriter but I didn't have any luck. I really appreciate any help.
Thank you in advance.