9

Possible Duplicate:
Xml validation using XSD schema

I have generated some XML using some C#. I need to see if that XML validates against an XSD file. Is there a way to do this in C#? If so, how do I do this?

Community
  • 1
  • 1
user208662
  • 10,869
  • 26
  • 73
  • 86

1 Answers1

19

See this question:

Xml validation using XSD schema

It shows that all you need to do is set the right option when creating your XmlReader:

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, xsdFilePath);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);

var reader = XmlReader.Create(source, settings);

You will now get information on validation errors in settings_ValidationEventHandler and the document load will be aborted if required.

Community
  • 1
  • 1
Simon Steele
  • 11,558
  • 4
  • 45
  • 67