using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Collections;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
public static void Test()
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);
var navigator = doc.CreateNavigator();
navigator.CheckValidity(schema, callback);
foreach (var exception in errors)
var node = (XObject)exception.SourceObject;
Console.WriteLine(exception);
Console.WriteLine("{0}: {1}", node.GetType(), node.ToString());
Assert.IsTrue(node != null, "node != null");
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: " + Environment.Version);
Console.WriteLine("Uncaught exception: ");