Sometimes there is requirement to convert/serialize an object into the XML format so that we can do some operation and manipulation based on the XML data. Here is the simple trick to convert the object data into XML format.
This piece of code will give you the result of Serialized XML by converting an object.
void SerializeXml<T>(string filePath, object objectToSerialize)
{
try
{
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(fs, objectToSerialize);
fs.Flush();
fs.Close();
fs.Dispose();
}
catch (Exception ex)
{
}
}
This piece of code will give you the result of Serialized XML by converting an object.
void SerializeXml<T>(string filePath, object objectToSerialize)
{
try
{
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(fs, objectToSerialize);
fs.Flush();
fs.Close();
fs.Dispose();
}
catch (Exception ex)
{
}
}