CSharp · Console
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Reflection;
using NUnit.Framework; //NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/69499790/net-core-3-1-api-to-accept-complext-xml-object
[DataContract(Name="SMTPSettings", Namespace = "")]
public class SMTPSettings
{
[DataMember(Name ="Host", Order = 1)]
public string Host { get; set; }
[DataMember(Name = "Port", Order = 2)]
public int Port { get; set; }
[DataMember(Name = "UserName", Order = 3)]
public string UserName { get; set; }
[DataMember(Name = "Password", Order = 4)]
public string Password { get; set; }
[DataMember(Name = "UseSSL", Order = 5)]
public string UseSSL { get; set; }
[DataMember(Name = "TestEmailAddress", Order = 6)]
public string TestEmailAddress { get; set; }
}
public static partial class DataContractSerializerHelper
{
public static string ToContractXml<T>(this T obj, DataContractSerializer serializer = null, XmlWriterSettings settings = null)
{
serializer = serializer ?? new DataContractSerializer(obj == null ? typeof(T) : obj.GetType());
using (var textWriter = new StringWriter())
{
settings = settings ?? new XmlWriterSettings { Indent = true };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
serializer.WriteObject(xmlWriter, obj);
}
return textWriter.ToString();
}
}
public static T FromContractXml<T>(string xml, DataContractSerializer serializer = null)
{
using (var textReader = new StringReader(xml ?? ""))
using (var xmlReader = XmlReader.Create(textReader))
{
return (T)(serializer ?? new DataContractSerializer(typeof(T))).ReadObject(xmlReader);
}
}
}
public class TestClass
{
public static void Test()
{
var xml = GetXml();
var dataContractObject = DataContractSerializerHelper.FromContractXml<SMTPSettings>(xml);
dataContractObject.Dump();
}
static string GetXml() => @"<?xml version=""1.0""?>
<SMTPSettings xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Host>mail.clubcompete.com</Host>
<Port>26</Port>
<UserName>[email protected]</UserName>
<Password>xxxxxxxxx</Password>
<UseSSL>false</UseSSL>
<TestEmailAddress>[email protected]</TestEmailAddress>
</SMTPSettings>
";
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
public static string GetNetCoreVersion()
{
//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
//
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
return null;
}
}