是什么导致XML文件中填充空字符? [英] What could cause an XML file to be filled with null characters?

查看:219
本文介绍了是什么导致XML文件中填充空字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个棘手的问题.我怀疑这需要文件系统的一些高级知识才能回答.

This is a tricky question. I suspect it will require some advanced knowledge of file systems to answer.

我有一个针对.NET Framework 4.0的WPF应用程序"App1".它具有一个Settings.settings文件,该文件会生成一个存储默认设置的标准App1.exe.config文件.用户修改设置时,修改将进入AppData\Roaming\MyCompany\App1\X.X.0.0\user.config.这是所有标准的.NET行为.但是,有时候,我们发现客户计算机上的user.config文件不是应该的,这会导致应用程序崩溃.

I have a WPF application, "App1," targeting .NET framework 4.0. It has a Settings.settings file that generates a standard App1.exe.config file where default settings are stored. When the user modifies settings, the modifications go in AppData\Roaming\MyCompany\App1\X.X.0.0\user.config. This is all standard .NET behavior. However, on occasion, we've discovered that the user.config file on a customer's machine isn't what it's supposed to be, which causes the application to crash.

问题看起来像这样:user.config的大小大约是用XML填充时的大小,但不是XML,而是一堆NUL字符.字符0一遍又一遍地重复.我们没有任何信息导致此文件修改.

The problem looks like this: user.config is about the size it should be if it were filled with XML, but instead of XML it's just a bunch of NUL characters. It's character 0 repeated over and over again. We have no information about what had occurred leading up to this file modification.

如果仅删除user.config,则可以在客户的设备上解决此问题,因为公共语言运行时将仅生成一个新的运行时.他们将丢失对设置所做的更改,但可以再次进行更改.

We can fix that problem on a customer's device if we just delete user.config because the Common Language Runtime will just generate a new one. They'll lose the changes they've made to the settings, but the changes can be made again.

但是,我在另一个WPF应用程序"App2"和另一个XML文件info.xml中遇到了此问题.这次有所不同,因为文件是由我自己的代码而不是CLR生成的.共同的主题是,它们都是C#WPF应用程序,都是XML文件,在两种情况下,我们都无法在测试中重现该问题.这可能与C#应用程序与XML文件或一般文件进行交互的方式有关吗?

However, I've encountered this problem in another WPF application, "App2," with another XML file, info.xml. This time it's different because the file is generated by my own code rather than by the CLR. The common themes are that both are C# WPF applications, both are XML files, and in both cases we are completely unable to reproduce the problem in our testing. Could this have something to do with the way C# applications interact with XML files or files in general?

我们不仅不能在当前的应用程序中重现该问题,而且我什至无法通过编写有意产生错误的自定义代码来重现该问题.我找不到单个XML序列化错误或文件访问错误,而该错误或错误导致文件填充为空.那么可能会发生什么呢?

Not only can we not reproduce the problem in our current applications, but I can't even reproduce the problem by writing custom code that generates errors on purpose. I can't find a single XML serialization error or file access error that results in a file that's filled with nulls. So what could be going on?

App1通过调用Upgrade()Save()并获取并设置属性来访问user.config.例如:

App1 accesses user.config by calling Upgrade() and Save() and by getting and setting the properties. For example:

if (Settings.Default.UpgradeRequired)
{
    Settings.Default.Upgrade();
    Settings.Default.UpgradeRequired = false;
    Settings.Default.Save();
}

App2通过对XML进行序列化和反序列化来访问info.xml:

App2 accesses info.xml by serializing and deserializing the XML:

public Info Deserialize(string xmlFile)
{
    if (File.Exists(xmlFile) == false)
    {
        return null;
    }

    XmlSerializer xmlReadSerializer = new XmlSerializer(typeof(Info));

    Info overview = null;

    using (StreamReader file = new StreamReader(xmlFile))
    {
        overview = (Info)xmlReadSerializer.Deserialize(file);
        file.Close();
    }

    return overview;
}

public void Serialize(Info infoObject, string fileName)
{
    XmlSerializer writer = new XmlSerializer(typeof(Info));

    using (StreamWriter fileWrite = new StreamWriter(fileName))
    {
        writer.Serialize(fileWrite, infoObject);
        fileWrite.Close();
    }
}

我们在Windows 7和Windows 10上都遇到了问题.在研究此问题时,我遇到了这篇文章,其中在Windows 8.1中遇到了相同的XML问题:

We've encountered the problem on both Windows 7 and Windows 10. When researching the problem, I came across this post where the same XML problem was encountered in Windows 8.1: Saved files sometime only contains NUL-characters

我是否可以在代码中进行某些更改以防止这种情况发生,或者该问题是否在.NET行为中太深了?

Is there something I could change in my code to prevent this, or is the problem too deep within the behavior of .NET?

在我看来,存在三种可能性:

It seems to me that there are three possibilities:

  1. CLR正在将空字符写入XML文件.
  2. 文件的内存地址指针被切换到另一个位置,而不移动文件内容.
  3. 文件系统试图将文件移动到另一个内存地址,并且文件内容被移动,但是指针没有被更新.

我觉得2和3比1更有可能.这就是为什么我说它可能需要文件系统的高级知识.

I feel like 2 and 3 are more likely than 1. This is why I said it may require advanced knowledge of file systems.

对于可能帮助我重现,修复或解决该问题的任何信息,我将不胜感激.谢谢!

I would greatly appreciate any information that might help me reproduce, fix, or work around the problem. Thank you!

推荐答案

此行为没有记录的原因,因为这种情况正在发生在用户身上,但没有人能说出这种奇怪情况的由来.

There is no documented reason for this behavior, as this is happening to users but nobody can tell the origin of this odd conditions.

这可能是CLR问题,尽管这不太可能发生,如果没有

It might be CLR problem, although this is a very unlikely, the CLR doesn't just write null characters and XML document cannot contain null characters if there's no xsi:nil defined for the nodes.

无论如何,解决该问题的唯一记录方法是使用以下代码行删除损坏的文件:

Anyway, the only documented way to fix this is to delete the corrupted file using this line of code:

try
{
     ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
}
catch (ConfigurationErrorsException ex)
{
    string filename = ex.Filename;
    _logger.Error(ex, "Cannot open config file");

    if (File.Exists(filename) == true)
    {
        _logger.Error("Config file {0} content:\n{1}", filename, File.ReadAllText(filename));
        File.Delete(filename);
        _logger.Error("Config file deleted");
        Properties.Settings.Default.Upgrade();
        // Properties.Settings.Default.Reload();
        // you could optionally restart the app instead
    }
    else
    {
        _logger.Error("Config file {0} does not exist", filename);
    }
}

它将使用Properties.Settings.Default.Upgrade();恢复user.config 再次没有空值.

It will restore the user.config using the Properties.Settings.Default.Upgrade(); again without null values.

这篇关于是什么导致XML文件中填充空字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆