using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
public static void Test()
static void DumpInvalidNodes(XElement el)
if (el.GetSchemaInfo().Validity != XmlSchemaValidity.Valid)
Console.WriteLine("Invalid Element {0}",
.Aggregate("", (s, i) => s + "/" + i.Name.ToString()));
foreach (XAttribute att in el.Attributes())
var si = att.GetSchemaInfo();
if (si != null && si.Validity != XmlSchemaValidity.Valid)
Console.WriteLine("Invalid Attribute {0}",
(s, i) => s + "/" + i.Name.ToString()) + "/@" + att.Name.ToString()
foreach (XElement child in el.Elements())
public static void FixXml(string xmlDoc)
XDocument doc = XDocument.Parse(xmlDoc);
XmlSchemaSet schema = new XmlSchemaSet();
using (var sr = new StringReader(GetXsd()))
using (var xmlReader = XmlReader.Create(sr))
schema.Add("", xmlReader);
var errors = new List<XmlSchemaValidationException>();
ValidationEventHandler callback = (sender, args) =>
var exception = (args.Exception as XmlSchemaValidationException);
doc.Validate(schema, callback, true);
foreach (var exception in errors)
Console.WriteLine(exception);
DumpInvalidNodes(doc.Root);
var xml = @"<shiporder numericvalue=""not a numeric value"" stringvalue=""ok"" unexpectedAttribute=""unexpected"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<orderperson>John Smith</orderperson>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
<title>Hide your heart</title>
var xsd = @"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<!-- definition of simple elements -->
<xs:element name=""orderperson"" type=""xs:string""/>
<xs:element name=""name"" type=""xs:string""/>
<xs:element name=""address"" type=""xs:string""/>
<xs:element name=""city"" type=""xs:string""/>
<xs:element name=""country"" type=""xs:string""/>
<xs:element name=""title"" type=""xs:string""/>
<xs:element name=""note"" type=""xs:string""/>
<xs:element name=""quantity"" type=""xs:positiveInteger""/>
<xs:element name=""price"" type=""xs:decimal""/>
<!-- definition of attributes -->
<xs:attribute name=""orderid"" type=""xs:string""/>
<xs:attribute name=""numericvalue"" type=""xs:long""/>
<xs:attribute name=""stringvalue"" type=""xs:string""/>
<!-- definition of complex elements -->
<xs:element name=""shipto"">
<xs:element ref=""name""/>
<xs:element ref=""address""/>
<xs:element ref=""city""/>
<xs:element ref=""country""/>
<xs:element name=""item"">
<xs:element ref=""title""/>
<xs:element ref=""note"" minOccurs=""0""/>
<xs:element ref=""quantity""/>
<xs:element ref=""price""/>
<xs:element name=""shiporder"">
<xs:element ref=""orderperson""/>
<xs:element ref=""shipto""/>
<xs:element ref=""item"" maxOccurs=""unbounded""/>
<xs:attribute ref=""orderid"" use=""required""/>
<xs:attribute ref=""numericvalue"" use=""required""/>
<xs:attribute ref=""stringvalue"" use=""required""/>
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];