function to serialize object to xml in c#
{
try
{
string xmlText;
Type objectType = item.GetType();
XmlSerializer xmlSerializer = new XmlSerializer(objectType);
MemoryStream memoryStream = new MemoryStream();
using (XmlTextWriter xmlTextWriter =
new XmlTextWriter(memoryStream, Encoding.UTF8) { Formatting = Formatting.Indented })
{
xmlSerializer.Serialize(xmlTextWriter, item);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlText = new UTF8Encoding().GetString(memoryStream.ToArray());
memoryStream.Dispose();
return xmlText;
}
}
catch (Exception e)
{
System.Diagnostics.Debug.Write(e.ToString());
return null;
}
}
No comments:
Post a Comment