I've got strange behaviour of the XmlReader. When using it for validation Xml against Xsd everythink works fine and how I expect. But when I try validate a Xsd file against another Xsd, it throws no validation exception.
For better explenation of my use case: I have an application where an user can upload a Xml file like data input. The user must upload a xsd file for already uploaded xml file and after that, the Xml file is validated against the Xsd file already uploaded. But in the case when the user upload new Xsd file as Xml file (Xsd is valid Xml) and then uploads another Xsd file for validation first Xsd file, I neeed to notify validation message, that Xml file (in this case first Xms file) is not valid against uploaded Xsd file (second Xsd file).
Here is my own "wraper" implelementation of XmlReader:
public static class XmlReader
{
public static XmlDocument GetSchemaDocument(Stream stream)
{
var schemaDocument = new XmlDocument();
var schemaDocumentReader = new XmlTextReader(stream);
schemaDocument.Load(schemaDocumentReader);
return schemaDocument;
}
public static XmlReaderSettings GetXmlReaderSettings(Stream stream)
{
var schemaSet = new XmlSchemaSet();
var schemaSetReader = new XmlTextReader(stream);
schemaSet.Add("", schemaSetReader);
var settings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema,
Schemas = schemaSet,
};
settings.ValidationEventHandler += ValidationEventHandler;
return settings;
}
public static XmlDocument GetXmlDocument(Stream stream, XmlReaderSettings settings)
{
var reader = System.Xml.XmlReader.Create(stream, settings);
var document = new XmlDocument();
document.Load(reader);
return document;
}
static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
if(e.Severity == XmlSeverityType.Error)
throw new ValidationException("Xml does not valid with Xsd.");
}
}
My question is why does XmlReader validate any Xsd file to another Xsd file as valid?
As Pawel wrote.
Solution is to use XmlSchemaValidationFlags.ReportValidationWarnings.