using System.Collections.Generic;
using System.Xml.Serialization;
public class CustomExpression
[XmlAttribute] public string callFormat;
[XmlAttribute] public string format;
[XmlAttribute] public string name;
[XmlAttribute] public string style;
[XmlElement("BinaryOp", typeof(BinaryOp)) ]
[XmlElement("SomeOtherOp", typeof(SomeOtherOp)) ]
public VzExpression child;
[XmlInclude(typeof(BinaryOp)) ]
public abstract class VzExpression {}
public class BinaryOp : VzExpression { }
public class SomeOtherOp : VzExpression { }
public static void Test()
var expression = new CustomExpression
var xml = expression.GetXml(omitStandardNamespaces : true);
public static class XmlSerializationHelper
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
using (var reader = new StringReader(xmlString))
return (T)(serial ?? new XmlSerializer(typeof(T))).Deserialize(reader);
public static string GetXml<T>(this T obj, XmlSerializer serializer = null, 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))
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
public static void Main()
Console.WriteLine("Environment version: {0} ({1}, {2}, NewLine: {3}).",
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion,
System.Text.Json.JsonSerializer.Serialize(Environment.NewLine));
Console.WriteLine("Failed with unhandled exception: ");