Insert new child node in an existing XML

asp.net, c#, xml

Solution

Try adding this at the end of your method:

originalXml.Save(Server.MapPath("xmlTechReportDetails.xml"));

I think it's because you did not save the file. That's why your changes are not retained.

Problem

Good day everyone. I would like to ask for help with my code. I have here an XML document containing the following. ``` <?xml version="1.0" encoding="utf-8" ?> <TechnicalReport> <Data quantity = "2" description ="myDesc" findings = "none" actiontaken = "none" /> </TechnicalReport> ``` What I would like to do here is to add another child node inside the . I have searched for so many websites about my problem but to no avail. For example, I will add another node, say: ``` <?xml version="1.0" encoding="utf-8" ?> <TechnicalReport> <Data quantity = "2" description ="myDesc" findings = "none" actiontaken = "none" /> <Data quantity = "3" description ="myDesc2" findings = "none2" actiontaken = "none3" /> </TechnicalReport> ``` I have successfully compiled and loaded the XML file into a Repeater control using an XMLDataSource, but when I do an insert from my form, the Repeater control does not update its contents, and even my XML file also does not update. Here's my C# code: ``` public void AddNewRecord() { //Load XML Schema XmlDocument originalXml = new XmlDocument(); originalXml.Load(Server.MapPath("xmlTechReportDetails.xml")); //Create the node name Technical Report XmlNode TechReport = originalXml.SelectSingleNode("TechnicalReport"); XmlNode Data = originalXml.CreateNode(XmlNodeType.Element, "Data", null); //Insert quantity XmlAttribute quantity = originalXml.CreateAttribute("quantity"); quantity.Value = txtQty.Text; //Insert description XmlAttribute description = originalXml.CreateAttribute("description"); description.Value = txtDescription.Text; //Insert findings XmlAttribute findings = originalXml.CreateAttribute("findings"); findings.Value = txtFindings.Text; //Insert actions taken. XmlAttribute actionTaken = originalXml.CreateAttribute("actiontaken"); actionTaken.Value = txtAction.Text; Data.Attributes.Append(quantity); Data.Attributes.Append(description); Data.Attributes.Append(findings); Data.Attributes.Append(actionTaken); TechReport.AppendChild(Data); } ``` Please help.

Original source