Quickly validating an XML document against an XML Schema

  • Thread starter Thread starter TP-Software
  • Start date Start date
T

TP-Software

Hi,

What's the fastest way to validate an XML document against an XML Schema?
All I need to know is wether or not the document is valid in respect to the
Schema

Could someone show me an example in C# ?

Thnx.
 
TP-Software said:
Hi,

What's the fastest way to validate an XML document against an XML Schema?
All I need to know is wether or not the document is valid in respect to the
Schema

XmlTextReader reader = new XmlTextReader(fileName);
XmlValidatingReader vReader = new XmlValidatingReader(reader);

vReader.ValidationType = ValidationType.Schema;

try
{
while(vReader.Read());
}
catch
{
// error
}

If the schema is not in the XML file, you can use:

vReader.Schemas.Add(...) to add a reference to the schema.
 
Back
Top