using System.Collections.Generic;
using System.Xml.Serialization;
public static implicit operator string(varString v)
return v == null ? null : (string)v.Value;
public static void Main()
var test1 = new varString { KeyWord = "key word", Value = "my value" };
Console.WriteLine(test1.GetXml(true));
var test2 = new varString { Value = "my value" };
Console.WriteLine(test2.GetXml(true));
public static class XmlSerializationHelper
public static T LoadFromXML<T>(this string xmlString)
using (StringReader reader = new StringReader(xmlString))
return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
public static string GetXml<T>(this T obj, bool omitStandardNamespaces = false)
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
ns = new XmlSerializerNamespaces();
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings() { Indent = true };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();