What could cause an XML file to be filled with null characters?

前端 未结 5 1476
忘掉有多难
忘掉有多难 2020-12-28 18:01

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

I have a WPF application, \"App1,\" targeting .NET framework

相关标签:
5条回答
  • 2020-12-28 18:31

    I have the same problem, there is an extra "NUL" character at the end of serialized xml file:

    I am using XMLWriter like this:

    using (var stringWriter = new Utf8StringWriter())
            {
                using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, IndentChars = "\t", NewLineChars = "\r\n", NewLineHandling = NewLineHandling.Replace }))
                {                    
                    xmlSerializer.Serialize(xmlWriter, data, nameSpaces);
                    xml =  stringWriter.ToString();
                    var xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(xml);
                    if (removeEmptyNodes)
                    {
                        RemoveEmptyNodes(xmlDocument);
                    }
                    xml = xmlDocument.InnerXml;
                }
            }
    
    0 讨论(0)
  • 2020-12-28 18:35

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

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

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

    0 讨论(0)
  • 2020-12-28 18:45

    It's well known that this can happen if there is power loss. This occurs after a cached write that extends a file (it can be a new or existing file), and power loss occurs shortly thereafter. In this scenario the file has 3 expected possible states when the machine comes back up:

    1) The file doesn't exist at all or has its original length, as if the write never happened.

    2) The file has the expected length as if the write happened, but the data is zeros.

    3) The file has the expected length and the correct data that was written.

    State 2 is what you are describing. It occurs because when you do the cached write, NTFS initially just extends the file size accordingly but leaves VDL (valid data length) untouched. Data beyond VDL always reads back as zeros. The data you were intending to write is sitting in memory in the file cache. It will eventually get written to disk, usually within a few seconds, and following that VDL will get advanced on disk to reflect the data written. If power loss occurs before the data is written or before VDL gets increased, you will end up in state 2.

    This is fairly easy to repro, for example by copying a file (the copy engine uses cached writes), and then immediately pulling the power plug on your computer.

    0 讨论(0)
  • 2020-12-28 18:50

    I had a similar problem and I was able to trace my problem to corrupted HDD.

    Description of my problem (all related informations):

    • Disk attached to mainboard (SATA):

      • SSD (system),

      • 3 * HDD.

        One of the HDD's had a bad blocks and there were even problems reading the disk structure (directories and file listing).

    • Operation system: Windows 7 x64

    • file system (on all disks): NTFS

    When the system tried to read or write to the corrupted disk (user request or automatic scan or any other reason) and the attempt failed, all write operations (to other disk's) were incorrect. The files created on system disk (mostly configuration files by another applications) were written and were valid (probably because the files were cashed in RAM) on direct check of file content.

    Unfortunately, after a restart, all the files (written after the failed write/read access on corrupted drive) had the correct size, but the content of the files was 'zero byte' (exactly like in your case).

    Try rule out hardware related problems. You can try to check 'copy' the file (after a change) to a different machine (upload to web/ftp). Or try to save specific content to a fixed file. When the check file on different will be correct, or when the fixed content file will be 'empty', the reason is probably on local machine. Try to change HW components, or reinstall the system.

    0 讨论(0)
  • 2020-12-28 18:53

    I ran into a similar issue but it was on a server. The server restarted while a program was writing to a file which caused the file to contain all null characters and become unusable to the program writing/reading from it.

    So the file looked like this:

    The logs showed that the server restarted:

    The corrupted file showed that it was last updated at the time of the restart:

    0 讨论(0)
提交回复
热议问题