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=””http://www.w3.org/2001/XMLSchema””>
<xs:element name=””address””>
<xs:complexType>
<xs:sequence>
<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””/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>”;
xml = @”<?xml version=””1.0″”?>
<address>
<name>John Smith</name>
<street>109 Abbey Close</street>
<city>Hayes</city>
<country> UK</country>
</address>”;
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
try{
while(vr.Read());
}catch{
Console.WriteLine(“Xml Document is not well-formed.”);
}finally{
vr.Close();
}