using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
public static void Test()
var dataSet = CreateDataSet();
TestXmlDocument(dataSet);
static void TestXmlDocument(DataSet dataSet)
var doc = new XmlDocument();
var navigator = doc.CreateNavigator();
using (var writer = navigator.AppendChild())
dataSet.WriteXml(writer);
Console.WriteLine("\nCreated {0}:", doc.GetType().Name);
Console.WriteLine(doc.GetOuterXml());
static void TestXDocument(DataSet dataSet)
var doc = new XDocument();
using (var writer = doc.CreateWriter())
dataSet.WriteXml(writer);
Console.WriteLine("\nCreated {0}:", doc.GetType().Name);
Console.WriteLine(doc.Root);
static string GetDataSetXsd()
var xsd = @"<?xml version=""1.0"" encoding=""utf-16""?>
<xs:schema id=""NewDataSet"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
<xs:element name=""NewDataSet"" msdata:IsDataSet=""true"" msdata:UseCurrentLocale=""true"">
<xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
<xs:element name=""Table"">
<xs:element name=""ProjectID"" type=""xs:string"" minOccurs=""0"" />
<xs:element name=""Title"" type=""xs:string"" minOccurs=""0"" />
<xs:element name=""ProjectType"" type=""xs:string"" minOccurs=""0"" />
public static DataSet CreateDataSet()
DataSet dataSet = new DataSet();
using (var reader = new StringReader(GetDataSetXsd()))
dataSet.ReadXmlSchema(reader);
for (int i = 0; i < 3; i++)
var row = dataSet.Tables["Table"].NewRow();
row["ProjectID"] = (i * 2).ToString();
row["Title"] = "Project Number " + i.ToString();
row["ProjectType"] = ((ProjectTypes)(i % Enum.GetNames(typeof(ProjectTypes)).Length)).ToString();
dataSet.Tables["Table"].Rows.Add(row);
dataSet.Tables["Table"].Rows.Add(dataSet.Tables["Table"].NewRow());
public static class XmlSerializationHelper
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
serial = serial ?? new XmlSerializer(typeof(T));
using (var reader = new StringReader(xmlString))
return (T)serial.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 class XmlAssert
public static void AreEqual(
Assert.IsTrue(XNode.DeepEquals(Normalize(expected), Normalize(actual)));
private static XElement Normalize(XElement element)
.OrderBy(a => a.Name.ToString()),
.OrderBy(a => a.Name.ToString())
.Select(e => Normalize(e)));
.OrderBy(a => a.Name.ToString()));
return new XElement(element.Name, element.Attributes()
.OrderBy(a => a.Name.ToString()), element.Value);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];