Serializing a Well-Known Class object to XML, to Deserialize later. - The Bumpy Blog
in

dotnet.org.za

South African .NET Developer Portal

The Bumpy Blog

Bumpy's Bedside Story

Serializing a Well-Known Class object to XML, to Deserialize later.

If you ever worked on Mobile USSD, you will know that it works exactly like a web request. We have a USSD session manager application (sort of like IIS), because USSD also needs to keep sessions for each time the user comes back with a response. Every USSD service that we add have new requirements and thus normally have a new class to be added as a reference so that the Session manager are aware of the data type it should hold in its session data.

This becomes a problem after a few services because of DLL versioning etc, etc. It became apparent that we needed to store the Class objects in a way that the Session Manager does not need to know what it is. I looked at XML as the answer.

I used a XML Doc to send the Serialized class to this Session manager so that the Session Manager do not need to know what object type it is. I therefor needed to parse these class objects to XML. I wrote these methods to help me with this. The only snag is that it must be well-known object types, like string, int, bool, etc. I started looking at XML attributes, but could not make a generic method for any datatype I can pass to it.

The methods look like this:

        public XmlDocument ParseToXML(ClassObject data)
        {
            XmlSerializer ser = new XmlSerializer(typeof(ClassObject ));
            System.Text.StringBuilder doc = new System.Text.StringBuilder();
            TextWriter tw = new StringWriter(doc);
            try
            {
                ser.Serialize(tw, data);
            }
            catch (XmlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
            tw.Close();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(doc.ToString());
            return xmldoc;
        }
        public ClassObject FromXML(XmlDocument xmldoc)
        {
            ClassObject data = new ClassObject();
            XmlSerializer ser = new XmlSerializer(typeof(ClassObject));
            XmlTextReader xmlreader = new XmlTextReader(xmldoc.OuterXml, XmlNodeType.Document, null);
            
            try
            {
                return (ClassObject)ser.Deserialize(xmlreader);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                xmlreader.Close();
            }
        }
 The Class Object can look like this:
        public class ClassObject
        {
            private int _property1;
            private int _property2;
            private int _property3;
            private string _property4;
            private ItemIndexer[] _listitemsindex = new ItemIndexer[0];
            public class ItemIndexer
            {
                private int _itemindexprop1;
                private int _itemindexprop2;
                public int ItemIndexProp1
                {
                    get {return _itemindexprop1;}
                    set {_itemindexprop1 = value;}
                }
                public int ItemIndexProp2
                {
                    get {return _itemindexprop2;}
                    set {_itemindexprop2 = value;}
                }            
            }
            
            public ClassObject()
            {
            }
            public int Property1
            {
                get {return _property1;}
                set {_property1 = value;}
            }
            public int Property2
            {
                get {return _property2;}
                set {_property2 = value;}
            }
            public int Property3
            {
                get {return _property3;}
                set {_property3 = value;}
            }
            public string Property4
            {
                get {return _property4;}
                set {_property4 = value;}
            }
            public ItemIndexer[] ItemIndexes
            {
                get    {return _listitemsindex;}
                set {_listitemsindex = value;}
            }
        }
And just by running ParseToXML method, you should get a XMLDoc to send the
OuterXML as a string to save to DB or anywhere else, and get the ClassObject back by running
the FromXML method.
Remember to add 

using System.Xml.Serialization;

in your code.

 
Published Oct 06 2004, 04:10 PM by deonvs
Filed under:

Comments

 

Chris Taylor said:

In terms of performance as well as memory consumption and just general coding practice I would consider the following.

1) Use a BinaryFormatter this can be nicely optimized for the primitive types
2) I you must have an XML format, rather store the XML string, the DOM is very costly.
3) In your solution ClassObject should be sealed
4) FromXml is unecesarily instantiating a ClassObject

For the XML solution this should do:
public string ToXml(ClassObject data)
{
XmlSerializer xmlser = new XmlSerializer(typeof(ClassObject));
StringWriter wr = new StringWriter();

xmlser.Serialize(wr, data);

return wr.ToString();
}

public ClassObject FromXml(string value)
{
XmlSerializer xmlser = new XmlSerializer(typeof(ClassObject));
StringReader rdr = new StringReader(value);

return (ClassObject)xmlser.Deserialize(rdr);
}

Of course the BinaryFormater would not even need to know the type because the type is encoded into the persistence stream so you would not have to use the 'well known type'.
October 10, 2004 5:52 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Powered by Community Server (Commercial Edition), by Telligent Systems