xml serialization in c# code example
Example: XML Serialization
public static string ToXmlString<T>(this T value, bool removeDefaultXmlNamespaces = true, bool omitXmlDeclaration = true, Encoding encoding = null) where T : class
{
XmlSerializerNamespaces namespaces = removeDefaultXmlNamespaces ? new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }) : null;
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = omitXmlDeclaration;
settings.CheckCharacters = false;
using (var stream = new StringWriterWithEncoding(encoding))
using (var writer = XmlWriter.Create(stream, settings))
{
var serializer = new XmlSerializer(value.GetType());
serializer.Serialize(writer, value, namespaces);
return stream.ToString();
}
}