title: XML File Contains Only Blank Element Called ObjectProxy - C# XML Serialization
body: <p>I have added XML Serialization into my project in order to store my object data in XML files.</p>
  
  <p>I have used the following helper class to achieve this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static class SerializerHelper
      {
          /// &lt;summary&gt;
          /// Serializes an object.
          /// &lt;/summary&gt;
          /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
          /// &lt;param name="serializableObject"&gt;&lt;/param&gt;
          /// &lt;param name="fileName"&gt;&lt;/param&gt;
  
          private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(
              "SerializerHelper.cs");
  
          public static void SerializeObject&lt;T&gt;(string filepath, T serializableObject)
          {
              if (serializableObject == null) { return; }
  
              try
              {
                  XmlDocument xmlDocument = new XmlDocument();
                  XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
                  using (MemoryStream stream = new MemoryStream())
                  {
                      serializer.Serialize(stream, serializableObject);
                      stream.Position = 0;
                      xmlDocument.Load(stream);
                      xmlDocument.Save(filepath);
                      stream.Close();
                  }
              }
              catch (Exception ex)
              {
                  //Log exception here
                  logger.Error("Error Serializing: " + ex.Message);
              }
          }
  
  
          /// &lt;summary&gt;
          /// Deserializes an xml file into an object list
          /// &lt;/summary&gt;
          /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
          /// &lt;param name="fileName"&gt;&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          public static T DeSerializeObject&lt;T&gt;(string filepath)
          {
              T objectOut = default(T);
  
              if (!System.IO.File.Exists(filepath)) return objectOut;
  
              try
              {
                  string attributeXml = string.Empty;
  
                  XmlDocument xmlDocument = new XmlDocument();
                  xmlDocument.Load(filepath);
                  string xmlString = xmlDocument.OuterXml;
  
                  using (StringReader read = new StringReader(xmlString))
                  {
                      Type outType = typeof(T);
  
                      XmlSerializer serializer = new XmlSerializer(outType);
                      using (XmlReader reader = new XmlTextReader(read))
                      {
                          objectOut = (T)serializer.Deserialize(reader);
                          reader.Close();
                      }
  
                      read.Close();
                  }
              }
              catch (Exception ex)
              {
                  //Log exception here
                  logger.Error("Error Deserializing: " + ex.Message);
              }
  
              return objectOut;
          }
      }
  </code></pre>
  
  <p>Inside my methods, anywhere that I create/delete/alter objects, I use code such as the below to serialize the object data:</p>
  
  <pre class="lang-cs prettyprint-override"><code>        //Increments the failed logon attempts counter
          public void incrementFailedLogonAttempts()
          {
              logonAttemptCounter.incrementFailedLogons();
  
              //Update the failed logon attempt counter in the XML Data Store
              SerializerHelper.SerializeObject(@"C:\Users\Michael"
              + @"\Google Drive\FDM Dev Course Content\Workspace\SystemAdmin\SystemAdmin\"
              + @"XML Data Store\LogonAttemptCounter.xml", logonAttemptCounter);
          }
  </code></pre>
  
  <p>However, after running all my unit tests, some of my XML files (which were serialized fine before running all tests) now look like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>&lt;?xml version="1.0"?&gt;
  &lt;ObjectProxy_4 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
  </code></pre>
  
  <p>Does anyone know what might be going wrong here? Or why it is going wrong?</p>
  
  <p>Any help would be greatly appreciated!</p>