using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot(ElementName = "Envelope", Namespace = "soapenv")]
[XmlInclude(typeof(Derived1))]
[XmlInclude(typeof(Derived2))]
[XmlInclude(typeof(Derived3))]
public class SoapEnvelope
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Namespaces { get; set; } = new XmlSerializerNamespaces(new[]
new XmlQualifiedName("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"),
new XmlQualifiedName("impl", "http://blhablhab.com/")
[XmlElement("Header", Namespace = "soapenv", IsNullable = true)]
public object Header { get; set; }
[XmlElement("Body", Namespace = "soapenv")]
public SoapBody Body { get; set; }
[XmlElement("InnerEnvelope")]
public TheBase Envelope { get; set; }
[XmlInclude(typeof(Derived1))]
[XmlInclude(typeof(Derived2))]
[XmlInclude(typeof(Derived3))]
public abstract class TheBase{ }
public class Derived1: TheBase
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Namespaces { get; set; } = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("sch", "http://www.vendorcompany.com/schemas") });
[XmlAttribute("Version")]
public string Version = "1.0";
[XmlElement("EnvelopeContext")]
public EnvelopeContext EnvelopeContext { get; set; }
[XmlElement("EnvelopeBodyList", Namespace = "sch")]
public EnvelopeBodyList EnvelopeBodyList { get; set; }
public class Derived2 : TheBase
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Namespaces { get; set; } = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("sch2", "http://www.vendorcompany.com/schemas") });
[XmlAttribute("Version")]
public string Version = "1.0";
[XmlElement("EnvelopeContext")]
public EnvelopeContext EnvelopeContext { get; set; }
[XmlElement("EnvelopeBodyList", Namespace = "sch2")]
public EnvelopeBodyList2 EnvelopeBodyList { get; set; }
public class Derived3 : TheBase
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Namespaces { get; set; } = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("sch3", "http://www.vendorcompany.com/schemas") });
[XmlAttribute("Version")]
public string Version = "1.0";
[XmlElement("EnvelopeContext")]
public EnvelopeContext EnvelopeContext { get; set; }
[XmlElement("EnvelopeBodyList", Namespace = "sch3")]
public EnvelopeBodyList3 EnvelopeBodyList { get; set; }
public class SerializationExtensions
public string ConvertRequestToXml(TheBase request)
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SoapEnvelope));
var envelope = new SoapEnvelope();
envelope.Body = new SoapBody();
envelope.Body.Envelope = request;
using (var stringWriter = new StringWriter())
xmlSerializer.Serialize(stringWriter, envelope);
returnString = stringWriter.ToString();
public static void Test()
var xml = new SerializationExtensions().ConvertRequestToXml(
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 string GetOuterXml(this XmlNode node, bool indent = true)
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings
OmitXmlDeclaration = true,
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
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: ");