Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Xml: XML serialization for complex object model

by the3factory 3/5/2008 9:06:00 AM

Compact FrameWork 2.0 doesn't provide Binary Serialization. And XML Serialization is just too simple to handle what I need: I have to deal with a complex topology of object model. Room A may be associated to Room B, and Room B to Room C, Room C to Room A, it makes a circle finally, just like this:

DrawTools2005Xmlable

So, I need a way to serialize such an object model to XML. Fortunately, I had worked out something in my spare time. I call it "xmlable", just like "Serializable" in Binary Serialization.

Using the code

I will introduce Alex Fr's DrawTools to show you how this has been done.

First, include IXmlabe.cs and Xmlable.cs to your project.

Derive the classes you want to serialize from interface IXmlable, and declare an attribute Xmlable for it, and implement 2 methods of IXmlable: FromXml and ToXml

Collapse
using Xmlable;
using System.Xml;
namespace DrawTools
{
[Xmlable("DrawObject", IsUnique = false)]
abstract class DrawObject : IXmlable
{
public DrawObject(){/*...*/} // Xmlable need a constructor without parameter.
public virtual void FromXml(XmlElement xmlEle, IXmlContext cnt)
{
//base.FromXml(xmlEle,cnt); //the derived classes should call parent class's FromXml
color = Color.Black;
try { color = Color.FromArgb(Int32.Parse(xmlEle.GetAttribute("color"))); }
catch (Exception) { }
penWidth = 1;
try { penWidth = Int32.Parse(xmlEle.GetAttribute("penWidth")); }
catch (Exception) { }
name = xmlEle.GetAttribute("name");
XmlElement assoEle = xmlEle["associate"];
if(null != assoEle)
{
objAsso = cnt.FromXml(assoEle.FirstChild as XmlElement) as DrawObject;
}
}
public virtual void ToXml(XmlElement xmlEle, IXmlContext cnt)
{
//base.ToXml(xmlEle,cnt); //the derived classes should call parent class's ToXml
XmlToolkit.SetAttribute(xmlEle, "color", color.ToArgb().ToString());
XmlToolkit.SetAttribute(xmlEle, "penWidth", penWidth.ToString());
XmlToolkit.SetAttribute(xmlEle, "name", name);
XmlElement assoEle = cnt.Document.CreateElement("associate");
XmlElement objEle = cnt.ToXml(objAsso);
if (null != objEle)
assoEle.AppendChild(objEle);
xmlEle.AppendChild(assoEle);
}
} 

[Xmlable("TagName", IsUnique = true)]
public class ClassA

means that "TagName" will be the tag name of the XML nodes of the ClassA, and IsUnique = true means whatever amount of references of a same object of type ClassA is serialized into a XML file, when the file is loaded, there would not be a lot of references refer to a lot of different objects.

Hope this will help...

You must have been confused by what i have written above.

Just download the project, run it, draw something, right click on them, set their properties and make them associate to each other. Go to menu "File->Save as XML", and open the saved file with text editors to have a look.

I haven't done much test after I add Xmlable to DrawTools. Let's just focus on how objects be serialized to XML.

Tags: ,

Xml

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0