Reading the values from and Writing to the XML File in C#

I want to read the values from the XML file after doing some modifications and I want to write it to the same file.
Note:- It should not read the commented values from XML

Web.config:-

<add key="InputDataFileName" value="InputData.xml" /> 


Here is my code,

string inputxmlPath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["InputDataFileName"];
            XmlReaderSettings readerSettings = new XmlReaderSettings();
            readerSettings.IgnoreComments = true;
            XmlReader reader = XmlReader.Create(inputxmlPath, readerSettings);
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            var ParentNode = doc.SelectSingleNode("//TestData"); //This is the root Node
            if (ParentNode.ChildNodes.Count > 0)
            {
                foreach (XmlNode child in ParentNode)
                {
                    string elementName = child.LocalName;
                    switch (elementName)
                    {
                        case "URL":
                            for (int i = 0; i < child.ChildNodes.Count; i++)
                            {
                                XmlNode node = child.ChildNodes[i].FirstChild;
                                node.InnerText = CMEURL;
                            }
                            break;
                        case "LoadBalanceList":
                            for (int i = 0; i < child.ChildNodes.Count; i++)
                            {
                                XmlNode node = child.ChildNodes[i].FirstChild;
                                node.InnerText = CMEURL;
                            }
                            break;
                        case "Activity":
                            for (int i = 0; i < child.ChildNodes.Count; i++)
                            {
                                XmlNode node = child.ChildNodes[i].FirstChild;
                                node.InnerText = CMEURL;
                            }
                            break;

                        default:
                            break;
                    }
                }
            }

            doc.Save(inputxmlPath); //Getting an exception here

Exception: IOException was unhandled by user code The process cannot access the file 'C:\Users\Desktop\Patching \source\Automation\InputData.xml' because it is being used by another process.

Getting an exception in the Save method
Not Sure what I did wrong, Could anyone help me
Thanks in advance

Jon Skeet
people
quotationmark

You're never closing the XmlReader, so it's still got an open file handle for reading - which means you can't write to the same file. You should put it in a using statement:

var doc = new XmlDocument();
var settings = new XmlReaderSettings { IgnoreComments = true };
using (var reader = XmlReader.Create(inputXmlPath, settings))
{
    doc.Load(reader);
}

I'd personally use XDocument instead of XmlDocument though:

XDocument doc;
var settings = new XmlReaderSettings { IgnoreComments = true };
using (var reader = XmlReader.Create(inputxmlPath, settings))
{
    doc = XDocument.Load(reader);
}

people

See more on this question at Stackoverflow