Simple Serialize and de-Serialize to and from XML. This sample takes a C# object and serializes to XML file. Also it reads contents from a XML file and de-serializes it.
Happy coding.
Cheers
Adam
public class CSerializeController
{
public bool Serialize(object InData, string FQXMLFileNametoSave)
{
bool
ToGobool = false;
StreamWriter sw = new StreamWriter(FQXMLFileNametoSave);
System.Xml.Serialization.XmlSerializer TheXmlSerializer = new
System.Xml.Serialization.XmlSerializer(InData.GetType());
try
{
TheXmlSerializer.Serialize(sw,
InData);
ToGobool = true;
}
catch
(Exception ex)
{
throw new Exception(ex.ToString() + " - Error in function
Serialize");
}
finally
{
sw.Close();
sw.Dispose();
}
return ToGobool;
}
public object DeSerialize(object InEmptyData, string FQXMLFileNametoLoad)
{
object ToGoobject = new object();
StreamReader sr = null;
try
{
sr = new StreamReader(FQXMLFileNametoLoad);
System.Xml.Serialization.XmlSerializer serializer = new
System.Xml.Serialization.XmlSerializer(InEmptyData.GetType());
ToGoobject = (object)serializer.Deserialize(sr);
}
catch
(Exception ex)
{
ToGoobject = null;
throw new Exception(ex.ToString() + " - Error in function
DeSerialize");
}
finally
{
if (sr != null)
{
sr.Close();
sr.Dispose();
}
}
return ToGoobject;
}
}
No comments:
Post a Comment