TODO:
Have you ever wanted to Serialize an object using C#? The method I created below, will allow you to pass in an object and turn it into an XML Document.
SOLUTION:
/// <summary>
///
/// </summary>
/// <param name="myObject"></param>
/// <returns></returns>
public static XmlDocument MakeXmlDocumentFromObject(Object myObject)
{
UTF8Encoding encoding = new UTF8Encoding();
XmlDocument returnObject = new XmlDocument();
try
{
//create our serializer
XmlSerializer xmlSerial = new XmlSerializer(myObject.GetType());
//get it....
using (MemoryStream memStream = new MemoryStream())
{
//serialize the object
xmlSerial.Serialize(memStream, myObject);
//load the XML now, and we will return it....
returnObject.LoadXml(encoding.GetString(memStream.GetBuffer()));
}
return returnObject;
}
catch (Exception)
{
return null;
}
}
NOTES:
There are no notes on this subject.