本文介绍了我怎样才能找出为什么我的XML文件已被使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用下面的代码将XML文件写入硬盘驱动器时,就可以使用以下代码:

        


when I write the XML file to hard drive using the following code

    XmlDocument doc = new XmlDocument();
    doc.Load("D:\\project\\data.xml");
    if (!Directory.Exists("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy")))
    {
        DirectoryInfo di = Directory.CreateDirectory("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy"));
    }
    XmlTextWriter writer = new XmlTextWriter("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy") + "\\" + System.DateTime.Now.ToString("HH-mm-ss") + ".xml", null);
    XmlTextWriter writerlatest = new XmlTextWriter("D:\\project\\data\\latest\\today.xml", null);
    writer.Formatting = Formatting.Indented;
    writerlatest.Formatting = Formatting.Indented;
    doc.Save(writer);
    doc.Save(writerlatest);
    doc = null;
    writer.Flush();
    writerlatest.Flush();

it writes the XML file as desired but after that when I try to read that XML file in the same asp.net page (code placed in a C# Code Behind file) using the following code, it gives an error

    string filename = "D:\\project\\data\\latest\\today.xml";

    XmlSerializer serializer = new XmlSerializer(typeof(searchResult));
    serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
    serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
    FileStream fs = new FileStream(filename, FileMode.Open);

the error is as follows

The process cannot access the file 'D:\project\data\latest\today.xml' because it is being used by another process.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.IOException: The process cannot access the file 'D:\project\data\latest\today.xml' because it is being used by another process.

EDIT: The file isn't being used by any other process

解决方案

Make sure you're closing your writer with a call like writer.Close();

这篇关于我怎样才能找出为什么我的XML文件已被使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:04
查看更多