Object - SerializeXml
An object extension method that serialize a string to XML.
public static void Main() { var obj = new List<string> {"Fizz", "Buzz"}; //C# Extension Method: Object - SerializeXml string result =obj.SerializeXml(); Console.WriteLine(result); }
View Source
using System.IO; using System.Xml.Serialization; public static partial class Extensions { /// <summary> /// An object extension method that serialize a string to XML. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>The string representation of the Xml Serialization.</returns> public static string SerializeXml(this object @this) { var xmlSerializer = new XmlSerializer(@this.GetType()); using (var stringWriter = new StringWriter()) { xmlSerializer.Serialize(stringWriter, @this); using (var streamReader = new StringReader(stringWriter.GetStringBuilder().ToString())) { return streamReader.ReadToEnd(); } } } }