If now the custom LayoutDocument is saved/loaded the custom type isn't stored in the XML file. The XML file contains a RootDocument instead of the custom derived class name.
Therefore saving/load a CUSTOM LayoutDocument in a LayoutDocumentFloatingWindow is not possible.
To solve this, I made a proposal for serializing LayoutDocumentFloatingWindow, LayoutFloatingWindow and LayoutAnchorableFloatingWindow. Serialization is performed by implementing the IXmlSerializable interface.
The nasty thing was that in the layout XML file I wanted the following structure:
<FloatingWindows>
<LayoutDocumentFloatingWindow>
<CustomLayoutDocument1 />
<LayoutDocumentFloatingWindow />
<LayoutDocumentFloatingWindow>
<CustomLayoutDocument2 />
<LayoutDocumentFloatingWindow />
<FloatingWindows />
Therefore I had to implement the IXmlSerializable in LayoutRoot.
See attached files with the solutions and an example of the resulting XML file.
Comments: ** Comment from web user: BoucherS **
Hi,
Your solution will work, but not in the "WpfApplication118" sample attached. The reason is that this sample has a RootPanel containing 2 children (a LayoutAnchorablePane and a LayoutDocumentPane).
Upon loading, your solution only reads the first child and then crashes in LayoutRoot.ReadRootPanel() at reader.ReadEndElement().
Here's a modification to your solution :
1) LayoutRoot.ReadRootPanel() will now read all RootPanel children and return a list of ILayoutPanelElement :
```
private List<ILayoutPanelElement> ReadRootPanel( XmlReader reader )
{
List<ILayoutPanelElement> result = new List<ILayoutPanelElement>();
string startElementName = reader.LocalName;
reader.Read();
if( reader.LocalName == startElementName && reader.NodeType == XmlNodeType.EndElement )
{
return null;
}
while( reader.NodeType == XmlNodeType.Whitespace )
{
reader.Read();
}
if( reader.LocalName == "RootPanel" )
{
reader.Read();
while( true )
{
//Read all RootPanel children
var element = ReadElement( reader ) as ILayoutPanelElement;
if( element != null )
{
result.Add( element );
}
else
{
break;
}
}
}
reader.ReadEndElement();
return result;
}
```
2) In LayoutRoot.ReadXml(), all children will now be added to the RootPanel :
```
public void ReadXml( XmlReader reader )
{
reader.MoveToContent();
if( reader.IsEmptyElement )
{
reader.Read();
return;
}
List<ILayoutPanelElement> layoutPanelElement = ReadRootPanel( reader );
if( layoutPanelElement != null )
{
//Create the RootPanel with the first child
RootPanel = new LayoutPanel( layoutPanelElement.First() );
//Add all children to RootPanel
for( int i = 1; i < layoutPanelElement.Count; ++i )
{
RootPanel.Children.Add( layoutPanelElement[ i ] );
}
}
........
}
```