title: What could cause an XML file to be filled with null characters?
body: <p>This is a tricky question. I suspect it will require some advanced knowledge of file systems to answer.</p>
  
  <p>I have a WPF application, "App1," targeting .NET framework 4.0. It has a <code>Settings.settings</code> file that generates a standard <code>App1.exe.config</code> file where default settings are stored. When the user modifies settings, the modifications go in <code>AppData\Roaming\MyCompany\App1\X.X.0.0\user.config</code>. This is all standard .NET behavior. However, on occasion, we've discovered that the <code>user.config</code> file on a customer's machine isn't what it's supposed to be, which causes the application to crash.</p>
  
  <p>The problem looks like this: <code>user.config</code> 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.</p>
  
  <p><a href="https://i.stack.imgur.com/n4Zex.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/n4Zex.jpg" alt="enter image description here"></a></p>
  
  <p>We can fix that problem on a customer's device if we just delete <code>user.config</code> 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.</p>
  
  <p>However, I've encountered this problem in another WPF application, "App2," with another XML file, <code>info.xml</code>. 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?</p>
  
  <p>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?</p>
  
  <p>App1 accesses <code>user.config</code> by calling <code>Upgrade()</code> and <code>Save()</code> and by getting and setting the properties. For example:</p>
  
  <pre><code>if (Settings.Default.UpgradeRequired)
  {
      Settings.Default.Upgrade();
      Settings.Default.UpgradeRequired = false;
      Settings.Default.Save();
  }
  </code></pre>
  
  <p>App2 accesses <code>info.xml</code> by serializing and deserializing the XML:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>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: <a href="https://stackoverflow.com/questions/25288460/saved-files-sometime-only-contains-nul-characters">Saved files sometime only contains NUL-characters</a></p>
  
  <p>Is there something I could change in my code to prevent this, or is the problem too deep within the behavior of .NET?</p>
  
  <p>It seems to me that there are three possibilities:</p>
  
  <ol>
  <li>The CLR is writing null characters to the XML files.</li>
  <li>The file's memory address pointer gets switched to another location without moving the file contents.</li>
  <li>The file system attempts to move the file to another memory address and the file contents get moved but the pointer doesn't get updated.</li>
  </ol>
  
  <p>I feel like 2 and 3 are more likely than 1. This is why I said it may require advanced knowledge of file systems.</p>
  
  <p>I would greatly appreciate any information that might help me reproduce, fix, or work around the problem. Thank you!</p>