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;
[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;
[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:
=\=\
<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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.