Simplest way to validate Xml against a Xml Schema – level 200

There are plenty of articles out that explaining how to validate Xml, but they all seem to use namespaces or have other factors in them.  I didn’t find one that used plain Xml with no namespace declaration, so I whipped up a little example.  I included the Xml and Xsd inline and load it using a StringReader, but for your purposes, they would probably be in physical files.

            string xsd;

            string xml;

            xsd = @”<xs:schema xmlns:xs=””””&gt;

                        <xs:element name=””address””>



                        <xs:element name=””name”” type=””xs:string””/>

                        <xs:element name=””street”” type=””xs:string””/>

                        <xs:element name=””city”” type=””xs:string””/>

                        <xs:element name=””country”” type=””xs:string””/>





            xml = @”<?xml version=””1.0″”?>


                        <name>John Smith</name>

                        <street>109 Abbey Close</street>


                        <country> UK</country>



            XmlValidatingReader   vr   = null;

            vr = new XmlValidatingReader(new XmlTextReader(new StringReader(xml)));

            vr.Schemas.Add(null, new XmlTextReader(new StringReader(xsd)));

            vr.ValidationType = ValidationType.Schema;

            vr.ValidationEventHandler += new ValidationEventHandler(vr_ValidationEventHandler);


            // Validate XML data





                Console.WriteLine(“Xml Document is not well-formed.”);