简体   繁体   中英

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

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);
}

You need to dispose of your reader before you try to write to the file:

XmlDocument doc = new XmlDocument();
using(var reader = XmlReader.Create(inputxmlPath, readerSettings))            
   doc.Load(reader);

Now you still have the reader open, so the reader is accessing the file, and when you call doc.Save() the XmlDocument tries to access the file at the same time. The using statement takes care of disposing the reader for you, so the file isn't being used by the reader anymore when you call Save() . It's always a good idea to use resources with a using statement, instead of manually closing/disposing a resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM