An easy code to serialize and de-serialize objects in .net….
public static object ConvertXmlToObject(string deserializedXml, System.Type ObjType)
{
XmlSerializer ser;
object obj = null;
try
{
ser = new XmlSerializer(ObjType);
StringReader stringReader;
stringReader = new StringReader(deserializedXml);
XmlTextReader xmlReader;
xmlReader = new XmlTextReader(stringReader);
obj = ser.Deserialize(xmlReader);
xmlReader.Close();
stringReader.Close();
}
catch (Exception exp)
{
Throw;
}
return obj;
}
public XmlDocument ConvertObjectToXmlDocument(object obj, System.Type objectType)
{
XmlDocument doc;
try
{
XmlSerializer s = new XmlSerializer(objectType);
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
s.Serialize(xw, obj);
doc = new XmlDocument();
doc.LoadXml(sw.ToString());
}
catch (Exception exp)
{
Throw;
}
return doc;
}
#trivedimehulk@gmail.com
No comments:
Post a Comment