Click here to Skip to main content
12,956,197 members (100,939 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 14 Aug 2010

Use XMLSerializer to Store a Shape's Information

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


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 
    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
    public new IFireComponent Tag
            return comp;
            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;

Here is an example of the saved XML data:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<ShapeList xmlns:xsi=\"\" 


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.


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


About the Author

Lewis Liu L
Software Developer
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
Petr Pechovic14-Aug-10 5:21
memberPetr Pechovic14-Aug-10 5:21 
GeneralRe: My vote of 2 Pin
LLewisLiu23-Aug-10 11:53
memberLLewisLiu23-Aug-10 11:53 

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

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

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