using System.Reflection.Emit;
using System.Xml.Serialization;
public static void Main()
const string schemaXml = @"<xs:schema targetNamespace=""http://something.com/xmlcourse""
elementFormDefault=""qualified""
xmlns=""http://something.com/xmlcourse""
xmlns:lexc=""http://something.com/xmlcourse""
xmlns:xs=""http://www.w3.org/2001/XMLSchema""
<!-- Type Definitions -->
<xs:simpleType name=""nonNegativeDecimal"">
<xs:restriction base=""xs:decimal"">
<xs:minInclusive value=""0"" />
<xs:simpleType name=""emailAddress"">
<xs:restriction base=""xs:string"">
<xs:pattern value=""[^@]+@[^\.]+\..+""/>
<xs:simpleType name=""phoneNumber"">
<xs:restriction base=""xs:string"">
<xs:pattern value=""[0-9]{3}[-\.][0-9]{3}[-\.][0-9]{4}""/>
<xs:simpleType name=""address"">
<xs:restriction base=""xs:string"">
<xs:enumeration value=""apartment""/>
<xs:enumeration value=""condominium""/>
<xs:enumeration value=""townhouse""/>
<xs:enumeration value=""duplex""/>
<xs:enumeration value=""house""/>
<xs:enumeration value=""residence""/>
<!-- Attributes are used for date and time on last_updated -->
<xs:complexType name=""dateAndTime"">
<xs:attribute name=""date"" type=""xs:date""></xs:attribute>
<xs:attribute name=""time"" type=""xs:time""></xs:attribute>
<xs:complexType name=""listing"">
<xs:element name=""id"" type=""xs:string"" minOccurs=""1"" maxOccurs=""1""></xs:element>
<xs:element name=""type"" type=""lexc:address"" minOccurs=""1"" maxOccurs=""1""></xs:element>
<xs:element name=""address"" type=""xs:string"" minOccurs=""1"" maxOccurs=""1""></xs:element>
<xs:element name=""contact_name"" type=""xs:string"" minOccurs=""0"" maxOccurs=""1""></xs:element>
<xs:element name=""contact_phone"" type=""lexc:phoneNumber"" minOccurs=""1"" maxOccurs=""3""></xs:element>
<xs:element name=""contact_email"" type=""lexc:emailAddress"" minOccurs=""0"" maxOccurs=""1""></xs:element>
<xs:element name=""rent"" type=""lexc:nonNegativeDecimal"" minOccurs=""1"" maxOccurs=""1""></xs:element>
<!-- Schema Instance Contents -->
<xs:element name=""housing"">
<xs:element name=""listings"">
<xs:element name=""listing"" type=""lexc:listing"" minOccurs=""0"" maxOccurs=""unbounded""></xs:element>
<xs:unique name=""uniqueId"">
<xs:selector xpath=""lexc:listing""></xs:selector>
<xs:field xpath=""@id""></xs:field>
<xs:element name=""last_updated"" type=""lexc:dateAndTime"" minOccurs=""1"" maxOccurs=""1""></xs:element>
const string xml = @"<housing xmlns=""http://something.com/xmlcourse"">
<type>wrongtype</type> <!-- Type does not exist -->
<address>556 Huckaby Lane</address>
<contact_name>John Spacer</contact_name>
<contact_phone>555-555-5555</contact_phone> <!-- Over Max of 3 Phone Numbers-->
<contact_phone>555-555-5555</contact_phone>
<contact_phone>555-555-5555</contact_phone>
<contact_phone>555-555-5555</contact_phone>
<contact_email>kestrel.gmail.com</contact_email> <!-- Wrong Format for Email Address -->
<address>123 Saskatoon Lane</address>
<contact_name>George Romero</contact_name>
<contact_phone>123.456.7890</contact_phone>
<contact_email>jasper@montypython.com</contact_email>
<last_updated date=""2017-05-15"" time=""12:12:12"" />
var schema = XmlSchema.Read(new StringReader(schemaXml), null);
var schemaSet = new XmlSchemaSet();
var settings = new XmlReaderSettings
ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
ValidationType = ValidationType.Schema,
settings.ValidationEventHandler += (s, e) => Console.WriteLine(e.Message);
using (var reader = XmlReader.Create(new StringReader(xml), settings))