title: 'System.NotSupportedException' in mscorlib.ni.dll Memory stream is not expandable
body: <p>I am getting the error "Exception thrown: 'System.NotSupportedException' in mscorlib.ni.dll Memory stream is not expandable" when trying to serialize and save an instance of a custom class object.</p>
  
  <p>Here are my saving/loading methods:</p>
  
  <pre><code>    public void SerializeObject&lt;T&gt;(T serializableObject, string fileName)
      {
          if (serializableObject == null) { return; }
  
          try
          {
              XmlDocument xmlDocument = new XmlDocument();
              XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
              using (MemoryStream stream = new MemoryStream())
              {
                  // convert string to stream
                  byte[] byteArray = Encoding.UTF8.GetBytes(fileName);
                  MemoryStream fileNameStream = new MemoryStream(byteArray);
  
                  serializer.Serialize(stream, serializableObject);
                  stream.Position = 0;
                  xmlDocument.Load(stream);
                  xmlDocument.Save(fileNameStream);
                  stream.Dispose();
              }
          }
          catch (Exception ex)
          {
              Debug.WriteLine(ex.Message);
          }
  
          Debug.WriteLine(TAG + serializableObject.ToString() + " saved");
      }
  
  
      public T DeSerializeObject&lt;T&gt;(string fileName)
      {
          if (string.IsNullOrEmpty(fileName)) { return default(T); }
  
          T objectOut = default(T);
  
          try
          {
              string attributeXml = string.Empty;
  
              // convert string to stream
              byte[] byteArray = Encoding.UTF8.GetBytes(fileName);
              MemoryStream stream = new MemoryStream(byteArray);
  
              XmlDocument xmlDocument = new XmlDocument();
              xmlDocument.Load(stream);
              string xmlString = xmlDocument.OuterXml;
  
              using (StringReader read = new StringReader(xmlString))
              {
                  Type outType = typeof(T);
  
                  XmlSerializer serializer = new XmlSerializer(outType);
                  using (XmlReader reader = XmlReader.Create(read))
                  {
                      objectOut = (T)serializer.Deserialize(reader);
                      reader.Dispose();
                  }
  
                  read.Dispose();
              }
          }
          catch (Exception ex)
          {
              Debug.WriteLine(ex.Message);
          }
  
          if (objectOut != null) Debug.WriteLine(TAG + objectOut.ToString() + " loaded");
  
          return objectOut;
      }
  </code></pre>
  
  <p>And here is the object class that I'm trying to save:</p>
  
  <pre><code>public class EntryDataType
  {
      readonly string TAG = "EntryDataType: ";
  
      private static int idCounter = -1;
      public int id;
      private EntryDataType parentEdt;
      public EntryDataType parentEdtProperty
      {
          get { return parentEdt; }
          set { parentEdt = value; }
      }
  
      // row control is not serializable, so ignore it when saving
      [XmlIgnore]
      public RowControl linkedRowControl;
      public int indent = -1;
      public int index = -1;
      public int linearIndex = -1;
      private bool completed = false;
      public bool completedProperty {
          get { return completed; }
          set
          {
              // set hidden state and set all children's hidden state, i.e. they will do the same
              completed = value;
              foreach (var item in childList)
              {
                  item.linkedRowControl.SetCompleted(value);
              }
          }
      }
  
      public ChildList&lt;EntryDataType&gt; childList;
      public bool bulletButtonChecked;
      public string textboxText;
  
      public EntryDataType()
      {
          // assign unique id to each entry
          id = idCounter;
          idCounter++;
  
          //Debug.WriteLine(TAG + "new entry " + id + " created");
  
          childList = new ChildList&lt;EntryDataType&gt;();
          childList.parentEdtOfChildListProperty = this;
      }
  
  }
  </code></pre>
  
  <p>I've already rewritten the class to eliminate it's constructor's parameters, and to ignore the unserializeable RowControl member. I am just learning .NET and c# so don't fully know what I'm doing yet; any help is greatly appreciated. Thanks :)</p>