SerializeToXml
Serializes objects to an xml string. (Does not provide error handling if the object is not serializable.)
Source
public static string SerializeToXml(this object obj)
{
XDocument doc = new XDocument();
using(XmlWriter xmlWriter = doc.CreateWriter())
{
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(xmlWriter, obj);
xmlWriter.Close();
}
return doc.ToString();
}
Example
string hi = "hellow world";
string xml = hi.SerializeToXml();
Console.WriteLine(xml);
Output:
<string>hellow world</string>