CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Threading;
using System.ComponentModel;
using System.Xml.Linq;
using System.Reflection;
using System.Data;
using System.Xml.Schema;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml.XPath;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Collections;
// https://stackoverflow.com/questions/40791596/object-model-with-various-type-inside
public abstract class RootBase
{
public Header header { get; set; }
[XmlIgnore]
// The following provides get-only access to the specific content of this root
public abstract ContentBase BasicContent { get; }
}
public class Header
{
public string elementA { get; set; }
public string elementB { get; set; }
}
[XmlRoot(ElementName = "root")]
public class Root<TContent> : RootBase
where TContent : ContentBase
{
public TContent content { get; set; }
public override ContentBase BasicContent { get { return content; } }
}
public abstract class ContentBase
{
public string elementC { get; set; }
}
public class ContentA : ContentBase
{
public VariousTypeA variousTypeA { get; set; }
}
public class ContentB : ContentBase
{
public VariousTypeB variousTypeB { get; set; }
}
public class VariousTypeA
{
public string itemA { get; set; }
public string itemB { get; set; }
public string itemC { get; set; }
}
public class VariousTypeB
{
public string itemZ { get; set; }
public string itemY { get; set; }
public string itemX { get; set; }
}
class TestClass
{
static string GetTypeAXml()
{
var xml = @"<root>
<header>
<elementA>Example</elementA>
<elementB>Test</elementB>
</header>
<content>
<elementC>foo</elementC>
<variousTypeA>
<itemA>Test</itemA>
<itemB>Test</itemB>
<itemC>Test</itemC>
</variousTypeA>
</content>
</root>";
return xml;
}
static string GetTypeBXml()
{
var xml = @"<root>
<header>
<elementA>Example</elementA>
<elementB>Test</elementB>
</header>
<content>
<elementC>foo</elementC>
<variousTypeB>
<itemZ>Test</itemZ>
<itemY>Test</itemY>
<itemX>Test</itemX>
</variousTypeB>
</content>
</root>";
return xml;
}
internal static void Test()
{
var xmlStringA = GetTypeAXml();
var rootA = (Root<ContentA>)new XmlSerializer(typeof(Root<ContentA>)).Deserialize(new StringReader(xmlStringA));
var newXmlA = rootA.GetXml(true);
Console.WriteLine("Deserialized and re-serialized {0}", rootA.GetType());
Console.WriteLine(newXmlA);
var xmlStringB = GetTypeBXml();
var rootB = (Root<ContentB>)new XmlSerializer(typeof(Root<ContentB>)).Deserialize(new StringReader(xmlStringB));
var newXmlB = rootB.GetXml(true);
Console.WriteLine("Deserialized and re-serialized {0}", rootB.GetType());
Console.WriteLine(newXmlB);
}
}
public class Program
{
public static void Main()
{
TestClass.Test();
}
}
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();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
}
using (var textWriter = new StringWriter())
{
var settings = new XmlWriterSettings() { Indent = true }; // For cosmetic purposes.
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
}
}
}