Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

Use XMLSerializer to Store a Shape's Information

3.50/5 (2 votes)
19 Aug 2010CPOL 12.5K   133  
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.

C#
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:

C#
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:

C#
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
<?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)