I have a C# class that I have inherited. I have successfully \"built\" the object. But I need to serialize the object to XML. Is there an easy way to do it?
It looks
public string ObjectToXML(object input)
{
try
{
var stringwriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(input.GetType());
serializer.Serialize(stringwriter, input);
return stringwriter.ToString();
}
catch (Exception ex)
{
if (ex.InnerException != null)
ex = ex.InnerException;
return "Could not convert: " + ex.Message;
}
}
//Usage
var res = ObjectToXML(obj)
You need to use following classes:
using System.IO;
using System.Xml;
using System.Xml.Serialization;