title: Serializing/deserializing the children of a WPF canvas to/from a XAML file
body: <p>I have a DragCanvas class which inherits from Canvas and provides functionality for drag&amp;drop, resizing and repositioning of elements. I'd like to be able to save/load the contents of the canvas to/from a XAML file.</p>
  
  <p>The export method looks like this:</p>
  
  <pre><code>public void ExportPanel()
      {
          string savedCanvasString = XamlWriter.Save(editCanvas);
          File.WriteAllText("Canvas.xaml", savedCanvasString);
      }
  </code></pre>
  
  <p>The code seems to be working fine, since I'm actually getting a Canvas.xaml file with a very long string.</p>
  
  <p>However, the import method is throwing an exception in the XamlReader.Load method. More especifically: "Add value to dictionary of type 'System.Windows.ResourceDictionary' threw an exception.". The code from the import method is partially based on <a href="http://msdn.microsoft.com/en-us/library/ms590398.aspx" rel="nofollow">this article</a>. Here's the code:</p>
  
  <pre><code>public void ImportPanel()
      {
          string savedCanvasString = File.ReadAllText("Canvas.xaml");
          StringReader stringReader = new StringReader(savedCanvasString);
          XmlReader xmlReader = XmlReader.Create(stringReader);
          DragCanvas savedCanvasChildren = (DragCanvas)XamlReader.Load(xmlReader); // The exception is being thrown here.
  
          // Assignment code should be here.
      }
  </code></pre>
  
  <p>Do you have any ideas of what am I doing wrong? Thanks in advance!</p>