使用FileSystemWatcher检测xml文件,使用Linq读取xml文件并提示结果错误“Root Element is Missing”

时间:2023-01-20 20:55:34

My application is already working it can detect the xml file and prompt the content of the xml file but sometimes it will prompt "Root element is missing", and sometimes also it is okay but when I open the xml file, it is ok, it has contents on it. How to solve this issue.

我的应用程序已经开始工作了,它可以检测xml文件并提示xml文件的内容,但有时它会提示“Root element is missing”,有时它也可以,但是当我打开xml文件时,它是ok的,上面有内容。如何解决这个问题。

Here is the screenshot of the error:

这里是错误的截图:

使用FileSystemWatcher检测xml文件,使用Linq读取xml文件并提示结果错误“Root Element is Missing”

Here is the code:

这是代码:

private void fileSystemWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        string invoice = "";
        using (var stream = System.IO.File.Open(e.FullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
        {
            var doc = System.Xml.Linq.XDocument.Load(stream);
            var transac = from r in doc.Descendants("Transaction")
                          select new
                          {
                              InvoiceNumber = r.Element("InvoiceNumber").Value,
                          };

            foreach (var i in transac)
            {
                invoice = i.InvoiceNumber;
            }
        }

        MessageBox.Show(invoice);
        fileSystemWatcher.EnableRaisingEvents = false;
    }

The error goes here var doc = System.Xml.Linq.XDocument.Load(stream);

错误出现在这里,var doc = System.Xml.Linq.XDocument.Load(流);

2 个解决方案

#1


3  

The FileSystemWatcher will raise the event very quickly when the file chanes. It is possible that the write operatrion has not finished before you read the file.

当文件发生变化时,文件系统监视程序将很快地引发事件。在读取文件之前,写操作数可能还没有完成。

To test that, add a Thread.Sleep(100); before you read the file as a quick workaround.

要测试它,添加一个Thread.Sleep(100);在快速浏览文件之前。

To get a better solution, do not open the file in shared mode, but try if you can open it exclusively. If this fails, wait some milliseconds and try again - until you can open the file exclusively. Also take care that the event might be triggered more than once - this depends on how the other task writes to the file.

要获得更好的解决方案,请不要在共享模式下打开文件,但是如果可以完全打开文件,请尝试这样做。如果失败,请等待几毫秒,然后再次尝试,直到您可以完全打开该文件。还要注意事件可能被多次触发——这取决于其他任务如何写入文件。

#2


2  

It is possible that there is a race condition when the file created and then written to in separate steps. Thus, occasionally you have an empty file when the created event fires which causes an exception. You should wait a short period after the Created event fires for a Changed event on the FileSystemWatcher. Also, note that there many be several Changed events before all content appears in the file if a significant amount is being written.

当文件创建并以单独的步骤写入时,可能存在竞争条件。因此,当创建的事件引发异常时,偶尔会有一个空文件。在创建的事件触发文件系统监视程序上的更改事件之后,您应该等待很短的时间。另外,请注意,如果要写入大量内容,那么在所有内容出现在文件中之前,会有许多更改事件。

#1


3  

The FileSystemWatcher will raise the event very quickly when the file chanes. It is possible that the write operatrion has not finished before you read the file.

当文件发生变化时,文件系统监视程序将很快地引发事件。在读取文件之前,写操作数可能还没有完成。

To test that, add a Thread.Sleep(100); before you read the file as a quick workaround.

要测试它,添加一个Thread.Sleep(100);在快速浏览文件之前。

To get a better solution, do not open the file in shared mode, but try if you can open it exclusively. If this fails, wait some milliseconds and try again - until you can open the file exclusively. Also take care that the event might be triggered more than once - this depends on how the other task writes to the file.

要获得更好的解决方案,请不要在共享模式下打开文件,但是如果可以完全打开文件,请尝试这样做。如果失败,请等待几毫秒,然后再次尝试,直到您可以完全打开该文件。还要注意事件可能被多次触发——这取决于其他任务如何写入文件。

#2


2  

It is possible that there is a race condition when the file created and then written to in separate steps. Thus, occasionally you have an empty file when the created event fires which causes an exception. You should wait a short period after the Created event fires for a Changed event on the FileSystemWatcher. Also, note that there many be several Changed events before all content appears in the file if a significant amount is being written.

当文件创建并以单独的步骤写入时,可能存在竞争条件。因此,当创建的事件引发异常时,偶尔会有一个空文件。在创建的事件触发文件系统监视程序上的更改事件之后,您应该等待很短的时间。另外,请注意,如果要写入大量内容,那么在所有内容出现在文件中之前,会有许多更改事件。