context: using System;
  using System.ComponentModel;
  using System.Drawing;
  using System.IO;
  using System.Text;
  using System.Xml;
  using System.Globalization;
  
  namespace WeifenLuo.WinFormsUI.Docking
  {
      partial class DockPanel
      {
          private static class Persistor
          {
              private const string ConfigFileVersion = "1.0";
              private static string[] CompatibleConfigFileVersions = { };
  
              private class DummyContent : DockContent
              {
              }
  
              private struct DockPanelStruct
              {
                  private double m_dockLeftPortion;
                  public double DockLeftPortion
                  {
                      get { return m_dockLeftPortion; }
                      set { m_dockLeftPortion = value; }
                  }
  
                  private double m_dockRightPortion;
                  public double DockRightPortion
                  {
                      get { return m_dockRightPortion; }
                      set { m_dockRightPortion = value; }
                  }
  
                  private double m_dockTopPortion;
                  public double DockTopPortion
                  {
                      get { return m_dockTopPortion; }
                      set { m_dockTopPortion = value; }
                  }
  
                  private double m_dockBottomPortion;
                  public double DockBottomPortion
                  {
                      get { return m_dockBottomPortion; }
                      set { m_dockBottomPortion = value; }
                  }
  
                  private int m_indexActiveDocumentPane;
                  public int IndexActiveDocumentPane
                  {
                      get { return m_indexActiveDocumentPane; }
                      set { m_indexActiveDocumentPane = value; }
                  }
  
                  private int m_indexActivePane;
                  public int IndexActivePane
                  {
                      get { return m_indexActivePane; }
                      set { m_indexActivePane = value; }
                  }
              }
  
              private struct ContentStruct
              {
                  private string m_persistString;
                  public string PersistString
                  {
                      get { return m_persistString; }
                      set { m_persistString = value; }
                  }
  
                  private double m_autoHidePortion;
                  public double AutoHidePortion
                  {
                      get { return m_autoHidePortion; }
                      set { m_autoHidePortion = value; }
                  }
  
                  private bool m_isHidden;
                  public bool IsHidden
                  {
                      get { return m_isHidden; }
                      set { m_isHidden = value; }
                  }
  
                  private bool m_isFloat;
                  public bool IsFloat
                  {
                      get { return m_isFloat; }
                      set { m_isFloat = value; }
                  }
              }
  
              private struct PaneStruct
              {
                  private DockState m_dockState;
                  public DockState DockState
                  {
                      get { return m_dockState; }
                      set { m_dockState = value; }
                  }
  
                  private int m_indexActiveContent;
                  public int IndexActiveContent
                  {
                      get { return m_indexActiveContent; }
                      set { m_indexActiveContent = value; }
                  }
  
                  private int[] m_indexContents;
                  public int[] IndexContents
                  {
                      get { return m_indexContents; }
                      set { m_indexContents = value; }
                  }
  
                  private int m_zOrderIndex;
                  public int ZOrderIndex
                  {
                      get { return m_zOrderIndex; }
                      set { m_zOrderIndex = value; }
                  }
              }
  
              private struct NestedPane
              {
                  private int m_indexPane;
                  public int IndexPane
                  {
                      get { return m_indexPane; }
                      set { m_indexPane = value; }
                  }
  
                  private int m_indexPrevPane;
                  public int IndexPrevPane
                  {
                      get { return m_indexPrevPane; }
                      set { m_indexPrevPane = value; }
                  }
  
                  private DockAlignment m_alignment;
                  public DockAlignment Alignment
                  {
                      get { return m_alignment; }
                      set { m_alignment = value; }
                  }
  
                  private double m_proportion;
                  public double Proportion
                  {
                      get { return m_proportion; }
                      set { m_proportion = value; }
                  }
              }
  
              private struct DockWindowStruct
              {
                  private DockState m_dockState;
                  public DockState DockState
                  {
                      get { return m_dockState; }
                      set { m_dockState = value; }
                  }
  
                  private int m_zOrderIndex;
                  public int ZOrderIndex
                  {
                      get { return m_zOrderIndex; }
                      set { m_zOrderIndex = value; }
                  }
  
                  private NestedPane[] m_nestedPanes;
                  public NestedPane[] NestedPanes
                  {
                      get { return m_nestedPanes; }
                      set { m_nestedPanes = value; }
                  }
              }
  
              private struct FloatWindowStruct
              {
                  private Rectangle m_bounds;
                  public Rectangle Bounds
                  {
                      get { return m_bounds; }
                      set { m_bounds = value; }
                  }
  
                  private int m_zOrderIndex;
                  public int ZOrderIndex
                  {
                      get { return m_zOrderIndex; }
                      set { m_zOrderIndex = value; }
                  }
  
                  private NestedPane[] m_nestedPanes;
                  public NestedPane[] NestedPanes
                  {
                      get { return m_nestedPanes; }
                      set { m_nestedPanes = value; }
                  }
              }
  
              public static void SaveAsXml(DockPanel dockPanel, string fileName)
              {
                  SaveAsXml(dockPanel, fileName, Encoding.Unicode);
              }
  
              public static void SaveAsXml(DockPanel dockPanel, string fileName, Encoding encoding)
              {
                  using (var fs = new FileStream(fileName, FileMode.Create))
                  {
                      try
                      {
                          SaveAsXml(dockPanel, fs, encoding);
                      }
                      finally
                      {
                          fs.Close();
                      }
                  }
              }
  
              public static void SaveAsXml(DockPanel dockPanel, Stream stream, Encoding encoding)
              {
                  SaveAsXml(dockPanel, stream, encoding, false);
              }
  
              public static void SaveAsXml(DockPanel dockPanel, Stream stream, Encoding encoding, bool upstream)
              {
                  XmlWriter xmlOut = XmlWriter.Create(stream, new XmlWriterSettings() { Encoding = encoding, Indent = true });
  
                  if (!upstream)
                      xmlOut.WriteStartDocument();
  
                  // Always begin file with identification and warning
                  xmlOut.WriteComment(Strings.DockPanel_Persistor_XmlFileComment1);
                  xmlOut.WriteComment(Strings.DockPanel_Persistor_XmlFileComment2);
  
                  // Associate a version number with the root element so that future version of the code
                  // will be able to be backwards compatible or at least recognise out of date versions
                  xmlOut.WriteStartElement("DockPanel");
                  xmlOut.WriteAttributeString("FormatVersion", ConfigFileVersion);
                  xmlOut.WriteAttributeString("DockLeftPortion", dockPanel.DockLeftPortion.ToString(CultureInfo.InvariantCulture));
                  xmlOut.WriteAttributeString("DockRightPortion", dockPanel.DockRightPortion.ToString(CultureInfo.InvariantCulture));
                  xmlOut.WriteAttributeString("DockTopPortion", dockPanel.DockTopPortion.ToString(CultureInfo.InvariantCulture));
                  xmlOut.WriteAttributeString("DockBottomPortion", dockPanel.DockBottomPortion.ToString(CultureInfo.InvariantCulture));
  
                  if (!Win32Helper.IsRunningOnMono)
                  {
                      xmlOut.WriteAttributeString("ActiveDocumentPane", dockPanel.Panes.IndexOf(dockPanel.ActiveDocumentPane).ToString(CultureInfo.InvariantCulture));
                      xmlOut.WriteAttributeString("ActivePane", dockPanel.Panes.IndexOf(dockPanel.ActivePane).ToString(CultureInfo.InvariantCulture));
                  }
  
                  // Contents
                  xmlOut.WriteStartElement("Contents");
                  xmlOut.WriteAttributeString("Count", dockPanel.Contents.Count.ToString(CultureInfo.InvariantCulture));
                  foreach (IDockContent content in dockPanel.Contents)
                  {
                      xmlOut.WriteStartElement("Content");
                      xmlOut.WriteAttributeString("ID", dockPanel.Contents.IndexOf(content).ToString(CultureInfo.InvariantCulture));
                      xmlOut.WriteAttributeString("PersistString", content.DockHandler.PersistString);
                      xmlOut.WriteAttributeString("AutoHidePortion", content.DockHandler.AutoHidePortion.ToString(CultureInfo.InvariantCulture));
                      xmlOut.WriteAttributeString("IsHidden", content.DockHandler.IsHidden.ToString(CultureInfo.InvariantCulture));
                      xmlOut.WriteAttributeString("IsFloat", content.DockHandler.IsFloat.ToString(CultureInfo.InvariantCulture));
                      xmlOut.WriteEndElement();
                  }
                  xmlOut.WriteEndElement();
  
                  // Panes
                  xmlOut.WriteStartElement("Panes");
                  xmlOut.WriteAttributeString("Count", dockPanel.Panes.Count.ToString(CultureInfo.InvariantCulture));
                  foreach (DockPane pane in dockPanel.Panes)
                  {
                      xmlOut.WriteStartElement("Pane");
                      xmlOut.WriteAttributeString("ID", dockPanel.Panes.IndexOf(pane).ToString(CultureInfo.InvariantCulture));
                      xmlOut.WriteAttributeString("DockState", pane.DockState.ToString());
                      xmlOut.WriteAttributeString("ActiveContent", dockPanel.Contents.IndexOf(pane.ActiveContent).ToString(CultureInfo.InvariantCulture));
                      xmlOut.WriteStartElement("Contents");
                      xmlOut.WriteAttributeString("Count", pane.Contents.Count.ToString(CultureInfo.InvariantCulture));
                      foreach (IDockContent content in pane.Contents)
                      {
                          xmlOut.WriteStartElement("Content");
                          xmlOut.WriteAttributeString("ID", pane.Contents.IndexOf(content).ToString(CultureInfo.InvariantCulture));
                          xmlOut.WriteAttributeString("RefID", dockPanel.Contents.IndexOf(content).ToString(CultureInfo.InvariantCulture));
                          xmlOut.WriteEndElement();
                      }
                      xmlOut.WriteEndElement();
                      xmlOut.WriteEndElement();
                  }
                  xmlOut.WriteEndElement();
  
                  // DockWindows
                  xmlOut.WriteStartElement("DockWindows");
                  int dockWindowId = 0;
                  foreach (DockWindow dw in dockPanel.DockWindows)
                  {
                      xmlOut.WriteStartElement("DockWindow");
                      xmlOut.WriteAttributeString("ID", dockWindowId.ToString(CultureInfo.InvariantCulture));
                      dockWindowId++;
                      xmlOut.WriteAttributeString("DockState", dw.DockState.ToString());
                      xmlOut.WriteAttributeString("ZOrderIndex", dockPanel.Controls.IndexOf(dw).ToString(CultureInfo.InvariantCulture));
                      xmlOut.WriteStartElement("NestedPanes");
                      xmlOut.WriteAttributeString("Count", dw.NestedPanes.Count.ToString(CultureInfo.InvariantCulture));
                      foreach (DockPane pane in dw.NestedPanes)
                      {
                          xmlOut.WriteStartElement("Pane");
                          xmlOut.WriteAttributeString("ID", dw.NestedPanes.IndexOf(pane).ToString(CultureInfo.InvariantCulture));
                          xmlOut.WriteAttributeString("RefID", dockPanel.Panes.IndexOf(pane).ToString(CultureInfo.InvariantCulture));
                          NestedDockingStatus status = pane.NestedDockingStatus;
                          xmlOut.WriteAttributeString("PrevPane", dockPanel.Panes.IndexOf(status.PreviousPane).ToString(CultureInfo.InvariantCulture));
                          xmlOut.WriteAttributeString("Alignment", status.Alignment.ToString());
                          xmlOut.WriteAttributeString("Proportion", status.Proportion.ToString(CultureInfo.InvariantCulture));
                          xmlOut.WriteEndElement();
                      }
                      xmlOut.WriteEndElement();
                      xmlOut.WriteEndElement();
                  }
                  xmlOut.WriteEndElement();
  
                  // FloatWindows
  
gt:                 RectangleConverter rectConverter = new RectangleConverter();