简体   繁体   English

在C#中从XML文件读取值并写入XML文件

[英]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. 我想在进行一些修改后从XML文件中读取值,并将其写入同一文件中。
Note:- It should not read the commented values from XML 注意:-不应从XML读取注释的值

Web.config:- 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. 异常:IOException未由用户代码处理该进程无法访问文件'C:\\ Users \\ Desktop \\ Patching \\ source \\ Automation \\ InputData.xml',因为该文件正在被另一个进程使用。

Getting an exception in the Save method Save方法中获取异常
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. 您永远不会关闭XmlReader ,因此它仍然具有打开的文件句柄以供读取-这意味着您无法写入同一文件。 You should put it in a using statement: 您应该将其放在using语句中:

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而不是XmlDocument

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. 现在您仍然可以打开阅读器,因此阅读器正在访问文件,并且当您调用doc.Save()XmlDocument尝试同时访问文件。 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() . using语句负责为您配置阅读器,因此,当您调用Save()时,阅读器不再使用该文件。 It's always a good idea to use resources with a using statement, instead of manually closing/disposing a resource. 将资源与using语句一起使用始终是一个好主意,而不是手动关闭/处置资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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