Knowledgebase: XML Serialization
Adding new properties to serialize
Posted by Admin - on 21 March 2014 11:20 AM

SciChart supports XML serialization via implementation of IXmlSerializable interface. This means that each serializable type has 2 implemented methods of this interface:

public virtual void WriteXml (XmlWriter writer)
{
    // Here we serialize properties we want to XmlWriter
}
public void ReadXml (XmlReader reader)
{
    // Here we reading and restoring state of properties from XmlReader
}

IXmlSerializable has also definition of GetSchema() method but this method should not be used.

Ok so lets take a look how to add new properties for XML serialization. In this tutorial we'll try to create new custom annotation which has additional property - Text.

XAML

<annotations:CustomAnnotation x:Class="Abt.Controls.SciChart.Wpf.MyAnnotation"
                              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                              xmlns:annotations="http://schemas.abtsoftware.co.uk/scichart"
                              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                              Name="Annotation"
                              d:DesignHeight="300"
                              d:DesignWidth="300"
                              mc:Ignorable="d">
    <Grid>
        <TextBlock Background="Red"
                   Foreground="Green"
                   Text="{Binding ElementName=Annotation,
                                  Path=Text}" />
    </Grid>
</annotations:CustomAnnotation> 

Cs

public partial class MyAnnotation : CustomAnnotation
{
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof (string), typeof (MyAnnotation), new PropertyMetadata(default(string)));

    public string Text
    {
        get { return (string) GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public override void WriteXml(XmlWriter writer)
    {
        // Serialization of main annotation properties
        base.WriteXml(writer);

        // Here we're adding serialization of added property
        writer.WriteAttributeString("Text",Text);
    }

    public override void ReadXml(XmlReader reader)
    {
        // Restoring of main annotation properties
        base.ReadXml(reader);

        // Restoring of Text property
        Text = reader["Text"];
    }
}
(2 vote(s))
Helpful
Not helpful

CONTACT US

Not sure where to start? Contact us, we are happy to help!


CONTACT US

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL. Email: Legal Company Number: 07430048, VAT Number: 101957725