public CanvasContent Deserialize()
        {
            var root = XElement.Load( myReader );

            double offsetX = Double.Parse( root.Attribute( "OffsetX" ).Value, CultureInfo.InvariantCulture );
            double offsetY = Double.Parse( root.Attribute( "OffsetY" ).Value, CultureInfo.InvariantCulture );

            var items = new List<DesignerItem>();

            var itemsXML = root.Elements( "DesignerItems" ).Elements( "DesignerItem" );
            foreach( var itemXML in itemsXML )
            {
                var id = new Guid( itemXML.Element( "ID" ).Value );
                var item = DeserializeDesignerItem( itemXML, id );

                item.SetConnectorDecoratorTemplate();

                items.Add( item );
            }

            var connections = new List<Connection>();

            var connectionsXML = root.Elements( "Connections" ).Elements( "Connection" );
            foreach( var connectionXML in connectionsXML )
            {
                var sourceID = new Guid( connectionXML.Element( "SourceID" ).Value );
                var sinkID = new Guid( connectionXML.Element( "SinkID" ).Value );

                var sourceConnectorName = connectionXML.Element( "SourceConnectorName" ).Value;
                var sinkConnectorName = connectionXML.Element( "SinkConnectorName" ).Value;

                var sourceConnector = GetConnector( items, sourceID, sourceConnectorName );
                var sinkConnector = GetConnector( items, sinkID, sinkConnectorName );

                var connection = new Connection( sourceConnector, sinkConnector );

                connection.SourceArrowSymbol = ( ArrowSymbol )Enum.Parse( typeof( ArrowSymbol ), connectionXML.Element( "SourceArrowSymbol" ).Value );
                connection.SinkArrowSymbol = ( ArrowSymbol )Enum.Parse( typeof( ArrowSymbol ), connectionXML.Element( "SinkArrowSymbol" ).Value );
                connection.IsDotted = connectionXML.Element( "IsDotted" ) != null ? bool.Parse( connectionXML.Element( "IsDotted" ).Value ) : false;

                Canvas.SetZIndex( connection, Int32.Parse( connectionXML.Element( "zIndex" ).Value ) );

                var propertiesContent = ConvertBladeNamespaces(connectionXML.Element("Properties").Value);

                using (var reader = XmlReader.Create(new StringReader(propertiesContent)))
                {
                    var properties = (ItemPropertyCollection)XamlReader.Load(reader);
                    connection.Properties = properties;
                }

                connection.Caption = connectionXML.Element( "Caption" ).Value;

                connections.Add( connection );
            }

            var canvas = new CanvasContent( items, connections );
            canvas.AddOffset( offsetX, offsetY );

            return canvas;
        }
 public void Save( CanvasContent content, string file )
 {
     using ( var writer = new StreamWriter( file ) )
     {
         var serializer = new CanvasXmlWriter( writer );
         serializer.Location = Path.GetDirectoryName( file );
         serializer.Serialize( content );
     }
 }
        public void Serialize( CanvasContent content )
        {
            var root = new XElement( "Root",
                new XAttribute( "OffsetX", content.Offset.X ),
                new XAttribute( "OffsetY", content.Offset.X ),
                new XAttribute( "Version", Version ),
                Serialize( content.Items ),
                Serialize( content.Connections ) );

            root.Save( myWriter );
        }
        public void CopyToClipboard( CanvasContent content )
        {
            using ( var writer = new StringWriter() )
            {
                var settings = new SerializationSettings();
                settings.RewriteIds = true;

                var serializer = new CanvasXmlWriter( writer, settings );
                serializer.Serialize( content );

                Clipboard.Clear();
                Clipboard.SetData( DataFormats.Xaml, writer.ToString() );
            }
        }
        private void CopyCurrentSelection()
        {
            var selectedDesignerItems = SelectionService.CurrentSelection.OfType<DesignerItem>();
            var selectedConnections = SelectionService.CurrentSelection.OfType<Connection>().ToList();

            foreach( Connection connection in this.Children.OfType<Connection>() )
            {
                if( !selectedConnections.Contains( connection ) )
                {
                    DesignerItem sourceItem = ( from item in selectedDesignerItems
                                                where item.ID == connection.Source.ParentDesignerItem.ID
                                                select item ).FirstOrDefault();

                    DesignerItem sinkItem = ( from item in selectedDesignerItems
                                              where item.ID == connection.Sink.ParentDesignerItem.ID
                                              select item ).FirstOrDefault();

                    if( sourceItem != null &&
                        sinkItem != null &&
                        BelongToSameGroup( sourceItem, sinkItem ) )
                    {
                        selectedConnections.Add( connection );
                    }
                }
            }

            var content = new CanvasContent( selectedDesignerItems, selectedConnections );
            content.AddOffset( 10, 10 );

            PersistenceService.CopyToClipboard( content );
        }
        private bool Save()
        {
            if( Filename == null )
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Filter = "WhiteBoard Files (*.bwb)|*.bwb|PNG (*.png)|*.png";

                var ret = saveFile.ShowDialog();
                if( ret == false )
                {
                    return false;
                }

                try
                {
                    if( saveFile.FilterIndex == 2 )
                    {
                        SelectionService.ClearSelection();

                        PersistenceService.SaveToPng( this, saveFile.FileName );
                        return false;
                    }
                    else
                    {
                        Filename = saveFile.FileName;
                    }
                }
                catch( Exception ex )
                {
                    MessageBox.Show( ex.StackTrace, ex.Message, MessageBoxButton.OK, MessageBoxImage.Error );
                }
            }

            try
            {
                var content = new CanvasContent( Children.OfType<DesignerItem>(), Children.OfType<Connection>() );
                PersistenceService.Save( content, Filename );

                return true;
            }
            catch( Exception ex )
            {
                MessageBox.Show( ex.StackTrace, ex.Message, MessageBoxButton.OK, MessageBoxImage.Error );
            }

            return false;
        }