Click here to Skip to main content
Click here to Skip to main content

Use XMLSerializer to Store a Shape's Information

, 19 Aug 2010
Rate this:
Please Sign up or sign in to vote.
Use an XML serializer and deserializer to store graphics configuration data

Introduction

When using the .NET Framework environment, developers need to store various information to the database. Some of the data are not the RDBMS sort of format, data like graphics drawing on a canvas, and it may contain all sorts of shapes, lines, colors, tooltips, notes, etc. And these kinds of information are changed often. To write code to save these kinds of data into database tables can be time consuming. We can save this information as an XML file though. We can then use an XML serializer and deserializer to open and save this information from/to the database servers.

We can define a class like the following. This class contains a list of the various serializable class objects.

using System.Xml;

using System.Xml.Serialization;
using System.Collections;
    //Every single type of class need to be serialized 
    //have to be declared like following 
    [XmlInclude(typeof(ZoneShape)),
    XmlInclude(typeof(DeviceShape)),
    XmlInclude(typeof(StarShape)),
    XmlInclude(typeof(PolyShape)), 
    XmlInclude(typeof(RectShape)),
    XmlInclude(typeof(DefaultSetting)), 
    ]
    [XmlRoot("ShapeList")]
    public class XMLShapes
    {
        public DefaultSetting mySettings;
        public List<FireShape> ShapeList;

        public XMLShapes()
        {
            ShapeList = new List<FireShape>();
            mySettings = new DefaultSetting();
        }
}

Above is the XMLShape class we will need to serialize.

Below is a snippet of the ZoneShape class:

public class ZoneShape : RectShape 
{
    public bool ShowNote { get; set; }
    public string Note { get; set; }
    public int ZoneNum { get; set; }

    private IFireComponent comp;
    //if you do not want a property to be
    //serialized use following attribute
    [XmlIgnore]
    public new IFireComponent Tag
    {
        get
        {
            return comp;
        }
        set
        {
            comp = value;
        }
    }

    public ZoneShape(Point pt)
        : base(pt)
    {
    }

    public string Caption
    {
        set { TextField.Caption = value; }
        get { return TextField.Caption; }
    }
}

To serialize/deserialize the XMLShape list class:

XmlSerializer xs = new XmlSerializer(xmlShapes.GetType());
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xmlStr));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xmlShapes = xs.Deserialize(memoryStream) as XMLShapes;
xmlShapes.Reflect();

Here is an example of the saved XML data:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<ShapeList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
           xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
  <mySettings>
    <DetectorWidth>30</DetectorWidth>
    <DetectorHeight>30</DetectorHeight>
    <ModuleWidth>30</ModuleWidth>
    <ModuleHeight>30</ModuleHeight>
    <TooltipSize>16</TooltipSize>
    <ShowExitPath>false</ShowExitPath>
  </mySettings>
</ShapeList>

We can save the above XML stream text into a database's BLOB column. We can then retrieve it back and deserialize it to the XMLShape list class like above.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Lewis Liu L
Software Developer
Australia Australia
If you think this article is useful, please donate using paypal:
https://www.paypal.com/au/webapps/mpp/make-online-payments
 
by using my email: yyiu002@hotmail.com

Comments and Discussions

 
GeneralMy vote of 2 PinmemberPetr Pechovic14-Aug-10 5:21 
GeneralRe: My vote of 2 PinmemberLLewisLiu23-Aug-10 11:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 19 Aug 2010
Article Copyright 2010 by Lewis Liu L
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid