Why is InvalideOperationException thrown when I try to serialize to XML an ObservableCollection containing hierarchical elements?
<p>I write WPF MVVM modular application using Prism 6 in MS VS 2015 Professional. When I try to serialize to XML file the hierarchical ObservableCollection using System.Xml.Serialization.XmlSerializer then System.InvalidOperationException is thrown in System.Xml.dll. Below is definition of the collection:</p>

<pre><code>public class Group : ProfileElementType
{
    . . . . . .
    private ObservableCollection&lt;ProfileElementType&gt; _childProfileElenents;
    [Browsable(false)]
    public ObservableCollection&lt;ProfileElementType&gt; ChildProfileElenents
    {
        get { return this._childProfileElenents; }
        set { this_childProfileElenents = value; }
    }
    public Group()
    {
        . . . . .
        this.ChildProfileElenents = new ObservableCollection&lt;ProfileElementType&gt;();
        . . . . .
    }
    . . . . .
}
</code></pre>

<p>Where [Browsable(false)] is System.ComponentModel.BrowsableAttribute. And now, let me give further details. Please see below the complete definition of ProfileElementType class and classes which are inherited from it. This is ProfileElementType class:</p>

<pre><code>[XmlInclude(typeof(Group))]
[XmlInclude(typeof(Register))]
public class ProfileElementType : BindableBase, IProfileElementType
{
    #region Fields
    private string _elementTypeName;
    #endregion

    #region Properties
    [Browsable(false)]
    public string ElementTypeName
    {
       get { return this._elementTypeName; }
       set { this._elementTypeName = value; }
    }
    #endregion

    #region IProfileElementType Implementation
    private Type _elementType;
    [Browsable(false)]
    public Type ElementType
    {
       get { return this._elementType; }
       set { this._elementType = value; }
    }
    #endregion
}
</code></pre>

<p>Where BindableBase class here is Prism.MVVM.BindableBase. And IProfileElementType
interface is shown below:</p>

<pre><code>public interface IProfileElementType
{
   Type ElementType { get; set; }
}
</code></pre>

<p>Below is Group and Register classes. These classes are inherited from ProfileElementType class. So this is Group class definition:</p>

<pre><code>public class Group : ProfileElementType
{
    #region Fields
    private string name;
    private string description;
    private ObservableCollection&lt;ProfileElementType&gt; _childProfileElenents;
    private Group parent;
    #endregion

    #region Constructors
    public Group()
    {
       this.ElementType = this.GetType();
       this.ElementTypeName = this.ElementType.Name;
       this.Name = "Group 1";
       this.ChildProfileElenents = new ObservableCollection&lt;ProfileElementType&gt;();
    }
    #endregion

    #region Properties
    [Display(Description = "The Name of this Group.", Name = "The Name")]
    public string Name
    {
        get { return this.name; }
        set { this.SetProperty(ref this.name, value); }
    }
    [Display(Description = "The Brief Description of this Group", Name = "The Description")]
    public string Description
    {
        get { return this.description; }
        set { this.SetProperty(ref this.description, value); }
    }
    /// &lt;summary&gt;
    /// The collection of elements which are the children elements of this group.
    /// These elements can be of Group class instances or Register class instances
    /// &lt;/summary&gt;
    [Browsable(false)]
    public ObservableCollection&lt;ProfileElementType&gt; ChildProfileElenents
    {
        get { return this._childProfileElenents; }
        set { this.childProfileElenents = value; }
    }
    /// &lt;summary&gt;
    /// Parent group whose ChildProfileElenents collection comprises this Group instance.
    /// &lt;/summary&gt;
    [Browsable(false)]
    public Group Parent
    {
        get { return this.parent; }
        set { this.parent = value; }
    }
    #endregion
}
</code></pre>

<p>And now, let me give you the definition of Register class:</p>

<pre><code>public class Register : ProfileElementType
{
   #region Fields
   private int number;
   private string name;
   private string description;
   private string currentValue;
   private string defaultValue;
   private string minimalValue;
   private string maximalValue;
   private string dataTypeName;
   private RegisterDataAccess accessToData;
   private Group parent;
   #endregion

   #region Constructors
   public Register()
   {
       this.ElementType = this.GetType();
       this.ElementTypeName = this.ElementType.Name;
       this.Name = "REgister 1";
       this.AccessToData = RegisterDataAccess.Read_Write;
   }
   #endregion

   #region Properties
   [Display(Description = "Register Address.", Name = "The Address", GroupName = "Common Information")]
   public int Number
   {
       get { return this.number; }
       set { this.SetProperty(ref this.number, value); }
   }
   [Display(Description = "Register Name.", Name = "The Name", GroupName = "Common Information")]
   public string Name
   {
      get { return this.name; }
      set { this.SetProperty(ref this.name, value); }
   }
   [Display(Description = "Breif description.", Name = "The Description", GroupName = "Common Information")]
   public string Description
   {
      get { return this.description; }
      set { this.SetProperty(ref this.description, value); }
   }
   [Display(Description = "Register current value.", Name = "Current Value", GroupName = "Data")]
   public string CurrentValue
   {
      get { return this.currentValue; }
      set { this.SetProperty(ref this.currentValue, value); }
   }
   [Display(Description = "Register default value.", Name = "Default value", GroupName = "Data")]
   public string DefaultValue
   {
      get { return this.defaultValue; }
      set { this.SetProperty(ref this.defaultValue, value); }
   }
   [Display(Description = "Register Minimal Value.", Name = "Minimal Value", GroupName = "Data")]
   public string MinimalValue
   {
      get { return this.minimalValue; }
      set { this.SetProperty(ref this.minimalValue, value); }
   }
   [Display(Description = "Register Maximal Value.", Name = "Maximal Value", GroupName = "Data")]
   public string MaximalValue
   {
      get { return this.maximalValue; }
      set { this.SetProperty(ref this.maximalValue, value); }
   }
   [Display(Description = "Register Data Type.", Name = "Data Type", GroupName = "Data")]
   public string DataTypeName
   {
      get { return this.dataTypeName; }
      set { this.SetProperty(ref this.dataTypeName, value); }
   }
   [Display(Description = "Access to Register Data.", Name = "Data Access", GroupName = "Data")]
   public RegisterDataAccess AccessToData
   {
      get { return this.accessToData; }
      set { this.SetProperty(ref this.accessToData, value); }
   }
   /// &lt;summary&gt;
   /// Parent group whose ChildProfileElenents collection comprises this Register instance.
   /// &lt;/summary&gt;
   [Browsable(false)]
   public Group Parent
   {
      get { return this.parent; }
      set { this.parent = value; }
   }
   #endregion
}
</code></pre>

<p>Where [Display(Description = "<em>description_string</em>", Name = "<em>name_string</em>")] is from System.ComponentModel.DataAnnotations namespace. Here is RegisterDataAccess
enumeration which is the type of accessToData field and AccessToData property in Register class:</p>

<pre><code>public enum RegisterDataAccess
{
   /// &lt;summary&gt;
   /// Access to data in register is read-only.
   /// &lt;/summary&gt;
   Read,
   /// &lt;summary&gt;
   /// Access to data in register is write-only.
   /// &lt;/summary&gt;
   Write,
   /// &lt;summary&gt;
   /// Read and write access to data in register.
   /// &lt;/summary&gt;
   Read_Write
}
</code></pre>

<p>Below is serializeProfile generic method definition in DeviceProfileViewModel class. This method executes XML serialization:</p>

<pre><code>public class DeviceProfileViewModel : BindableBase, IConfirmNavigationRequest
{
   . . . . . . .
   private string PathToXmlRepository;
   private string XmlRepositoryFileName = "XmlRepository.XML";
   private Group _profileTreeRoot;
   public Group ProfileTreeRoot
   {
      get { return this._profileTreeRoot; }
      set { this.SetProperty(ref this._profileTreeRoot, value); }
   }
   . . . . . . .
   public DeviceProfileViewModel()
   {
      . . . . . . .
      this.ProfileTreeRoot = new Group();
      this.ProfileTreeRoot.ChildProfileElenents.CollectionChanged += this.ChildProfileElenents_CollectionChanged;
      this.PathToXmlRepository = Path.GetFullPath("../../XML_DeviseProfileRepository");
      . . . . . . .
   }

   /// &lt;summary&gt;
   /// Executes XML serialization.
   /// &lt;/summary&gt;
   /// &lt;typeparam name="T"&gt;Type of serialized element&lt;/typeparam&gt;
   /// &lt;param name="parameter"&gt;Serialized element itself&lt;/param&gt;
   private void serializeProfile&lt;T&gt;(T parameter)
   {
      string pathToXml = Path.Combine(this.PathToXmlRepository, this.XmlRepositoryFileName);
      if (File.Exists(pathToXml))
         File.Delete(pathToXml);
      XmlSerializer xs = new XmlSerializer(typeof(T));

      using (StreamWriter wr = new StreamWriter(pathToXml))
      {
          xs.Serialize(wr, parameter);
      }
   }
   . . . . . . . .
}
</code></pre>

<p>Below is serializeProfile() method call in DeviceProfileViewModel class:</p>

<pre><code>ObservableCollection&lt;ProfileElementType&gt; coll = new ObservableCollection&lt;ProfileElementType&gt;(this.ProfileTreeRoot.ChildProfileElenents);
this.serializeProfile(coll);
</code></pre>

<p>I do assignment above because this.ProfileTreeRoot.ChildProfileElenents ObservableCollection is used in data binding as ItemsSource for telerik:RadTreeView in the view. You can see a device profile tree (group and registers) on the picture below:</p>

<p><a href="https://i.stack.imgur.com/2GB06.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2GB06.png" alt="enter image description here"></a></p>

<p>So this.ProfileTreeRoot here is the instance of Group class. Execution of </p>

<pre><code>xs.Serialize(wr, parameter);
</code></pre>

<p>line of code in serializeProfile() method throws InvalideOperationException exception in System.Xml.dll and an empty XML file is created in the specified path. What should I do to eliminate this error? Your help will be greatly appreciated. HELP me please serialize such a collection to XML file!!!</p>