开发者

Serialization inherited from Canvas class

I have problem with serialization inherited from Canvas class. I am looking for a few days longer solution. I tried XMLSerializer, XAMLWriter, now tries to use DataContractSerializer.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Rectangle rectangle = new Rectangle();
        rectangle.Width = 20;
        rectangle.Height = 30;

        Polyline polyLine = new Polyline();
        PointCollection pointCollection = new PointCollection(){new Point(10,10), new Point(30,30), new Point(50,10)};
        polyLine.Points = pointCollection;

        NewCanvas newCanvas = new NewCanvas("test", rectangle, polyLine);
        newCanvas.Width = 120;
        newCanvas.Height = 150;

        newCanvas.SaveToXML(@"Save.xml");
    }
}

[Serializable]
public class NewCanvas : Canvas, ISerializable
{
    private string _name;
    private Rectangle _rectangle;
    private Polyline _polyLine;

    public NewCanvas(string name, Rectangle rectangle, Polyline polyLine)
    {
        _name = name;
        _rectangle = rectangle;
        _polyLine = polyLine;
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("BaseProperties", XamlWriter.Save(this));
        info.AddValue("Name", _name);
        info.AddValue("Rectangle", XamlWriter.Save(_rectangle));
        info.AddValue("PolyLine", XamlWriter.Save(_polyLine));
    }

    public NewCanvas(SerializationInfo info, StreamingContext context)
    {
        //Deserialization implement
    }

    public void SaveToXML(string fileName)
    {
        DataContractSerializer ser = new DataContractSerializer(typeof(NewCanvas));
        XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };

        using (XmlWriter writer = XmlWriter.Create(fileName开发者_Python百科, settings))
        {
            ser.WriteObject(writer, this);
            writer.Close();
        }
    }
}

Above code generates:

<?xml version="1.0" encoding="utf-8"?>
<NewCanvas xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.datacontract.org/2004/07/Serializacja">
  <BaseProperties i:type="x:string" xmlns="">&lt;NewCanvas Width="120" Height="150" xmlns="clr-namespace:Serializacja;assembly=Serializacja" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /&gt;</BaseProperties>
  <Name i:type="x:string" xmlns="">test</Name>
  <Rectangle i:type="x:string" xmlns="">&lt;Rectangle Width="20" Height="30" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /&gt;</Rectangle>
  <PolyLine i:type="x:string" xmlns="">&lt;Polyline Points="10,10 30,30 50,10" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /&gt;</PolyLine>
</NewCanvas>

I want to get something like this, without these redudant namespace xlmns="" and and without repeated <Rectangle ...>Rectangle ...</Rectangle>

<?xml version="1.0" encoding="utf-8"?>
<NewCanvas>
 <BaseProperties Width="120" Height="150"/>
 <Name>test</Name>
 <Rectangle Width="20" Height="30"/>
 <Polyline Points="10,10 30,30 50,10"/>
</NewCanvas>

Do you have any ideas how I can accomplish this?


What are you trying to do? There is very big difference between saving some data to XML and serializing entity.

Serialization allows you to store state of the object and create instance with the same state when you deserialize the object from stored data. What you have described is not serialization because you will not be able to get the instance from that data. To get the data you need to serialize content of parent class (whole hierarchy) as well and none of them is serializable by DataContractSerializer (and probably by none serializer).

So if you want just to save some data simply use XmlWriter's methods directly or any other approach like XmlDocument or XElement.

Btw. DataContractSerializer doesn't offer full control over generated XML. For example it doesn't support XML attributes. You must use XmlSerializer if you want to use attributes and you must implement IXmlSerializable to have full control over XML serialization (that would be needed for your Points serialization).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜