Disclaimer: I am a consultant at Amazon Web Services, and this is my personal blog. The opinions expressed here are solely mine and do not reflect the views of Amazon Web Services (AWS). Any statements made should not be considered official endorsements or statements by AWS.
You can use below method to serialize an object to XML string.
Namespaces required:
using System.Xml.Serialization;
using System.IO;
Method definition:
public string GetXMLString<T>(T objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
using (StringWriter stringWriter = new System.IO.StringWriter())
{
xmlSerializer.Serialize(stringWriter, objectToSerialize);
return stringWriter.ToString();
}
}