exception occurs while saving XML using c#



I am trying to update an existing XML file by adding a new child node using c#. Everything is OK if I save it by new name but I want to update the same file and while doing it, got the following exception:



System.IO.IOException:Process cannot access the file... because it is being used by another process



Here is my code: (I am trying to add a new default node)



XmlDocument doc = new XmlDocument();
string path = @"C:\Debug\default.xml";
doc.Load(path);
XmlNode NName = doc.CreateElement("default");

XmlNode SNO = doc.CreateElement("SNo");
SNO.InnerText = "2";
NName.AppendChild(SNO);

doc.DocumentElement.AppendChild(NName);

doc.Save(path);


Also XML file:



<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<default>
<SNo>1</SNo>
</default>
</NewDataSet>

No comments:

Post a Comment