[Question] Using ReplaceChild


I was wondering if someone knows how to properly use ReplaceChild and point out how I'm using it wrong.

private void SaveFile() { XmlDocument doc=new XmlDocument(); doc.Load("C:\\Test.xml"); XmlNode Vehicles=doc.SelectSingleNode("//Vehicles"); XmlNode oldData=doc.SelectSingleNode("//Bus01"); XmlElement Admin=doc.CreateElement("Bus01"); XmlElement Serial=doc.CreateElement("Serial"); if(oldData!=null) { Vehicles.ReplaceChild(Admin,oldData); } else { Admin.AppendChild(Serial); Vehicles.AppendChild(Admin); } doc.Save("C:\\Test.xml"); } 

If the Test.xml doesn't exist, it adds an Admin node and adds Serial node under Admin node. If it exists, it replaces the previous Admin node with whatever I enter into the textbox. But for some reason, ReplaceChild is not working properly; it overwrites Test.xml with nothing in the node.

What am I doing wrong?

by asdf5hwerg via /r/csharp

Leave a Reply