I'm using the following code to validate an XML file using an XSD schema file.

It basically works. However, while I'm trying to build a list of any validation errors, I find that any validation error that occurs raises an exception, causing no further validation errors to be detected.

I'm actually running this in LINQPad. Can anyone see what I'm missing?

Error occurred while loading [], line 4 position 17
The 'http://tempuri.org/XMLSchema.xsd:tryme' element is invalid - The value 'tryme1' is invalid according to its datatype 'String' - The Pattern constraint failed.
Error occurred while loading [], line 5 position 17
The 'http://tempuri.org/XMLSchema.xsd:tryme' element is invalid - The value 'tryme1' is invalid according to its datatype 'String' - The Pattern constraint failed.
Error occurred while loading [], line 8 position 18
The 'http://tempuri.org/XMLSchema.xsd:stillwrong' element is invalid - The value 'a' is invalid according to its datatype 'Int' - The string 'a' is not a valid Int32 value.
Document1.xml is XSD 1.0 invalid.

However, if you remove the &lt;wrong/> element from your sample, i.e.:

Error occurred while loading [], line 4 position 17
The 'http://tempuri.org/XMLSchema.xsd:tryme' element is invalid - The value 'tryme1' is invalid according to its datatype 'String' - The Pattern constraint failed.
Error occurred while loading [], line 5 position 17
The 'http://tempuri.org/XMLSchema.xsd:tryme' element is invalid - The value 'tryme1' is invalid according to its datatype 'String' - The Pattern constraint failed.
Error occurred while loading [], line 7 position 4
The element 'really' in namespace 'http://tempuri.org/XMLSchema.xsd' has invalid child element 'stillwrong' in namespace 'http://tempuri.org/XMLSchema.xsd'. List of possible elements expected: 'wrong' in namespace 'http://tempuri.org/XMLSchema.xsd'.
Document1.xml is XSD 1.0 invalid.

While the number is the same, a .NET validator (at least the stock one) will not complain about &lt;stillwrong> now since it doesn't really know which XSD node to match it with.

The point is there may be errors which will cause the validator to abandon doing stuff, thus showing as if it skips what some may expect not to.

If, for the scenarios I've posted, your code gets all the errors as listed, then your code is all that it can be on .NET with the built in validator. If you're not getting all of what I've listed, then I missed on your issue as well.

Well, thanks for the detailed answer. But I think my question was more about throwing an exception, which prevents any further nodes from being examined. The assumption in your code above seems to be that no exception is thrown. Rather, the validation errors are reported and stored as expected.
–
Jonathan WoodFeb 27 '13 at 18:56

Validation errors are captured through delegates - not exceptions. Reading an XML through a validating reader will not throw an exception (on .NET) as long as the XML is well formed. I know that I am repeating myself, but on .NET validating XML will not throw exceptions (unless a custom handler does so). It must be then that if you're getting an exception, it is because the XML is not well formed or some other funky stuff (possibly I don't know about) is going on in there.
–
Petru GardeaFeb 27 '13 at 19:58

Yes, the issue appears to be syntactic vs. semantic errors. The former throw an exception while the latter does not.
–
Jonathan WoodFeb 27 '13 at 20:05