using System.Xml.Serialization;
public static class Program
public static void Main(string[] args)
SomeString = "someValue",
var other = new OtherClass
OtherString = "someValue",
Console.WriteLine(ToString(some));
Console.WriteLine(ToString(other));
private static string ToString(Data data)
var ser = new XmlSerializer(typeof(Data));
using (var writer = new StringWriter())
ser.Serialize(writer, data);
return writer.ToString();
[XmlRoot("data", Namespace = "http://comp.com/types")]
[XmlInclude(typeof(SomeClass))]
[XmlInclude(typeof(OtherClass))]
public abstract class Data
[XmlType(Namespace = "http://comp.com/types")]
public class SomeClass : Data
public string SomeString { get; set; }
public int SomeInt { get; set; }
[XmlType(Namespace = "http://comp.com/types")]
public class OtherClass : Data
public string OtherString { get; set; }
public int OtherInt { get; set; }