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.