Search code examples
c#xmlxsd.net-corexml-validation

Validating xml against an xsd that has include and import in c#


I would like to cache the xsd then perform validation against it rather than loading xsd every time for any xml in order to increase performance. However, I could not manage to do it. My guess is that the compilation does not add the include and import elements of the xsd files that is why I get the error below.

Here are the steps:

First I added the xsd file to XmlSchemaSet

public XmlSchemaSet SchemaSet;
public XmlValidator()
{
    this.SchemaSet = new XmlSchemaSet();
    using (XmlReader xsd = XmlReader.Create(xsdPath))
    {
        this.SchemaSet.Add(null, xsd);
    }
    this.SchemaSet.Compile();
}

Then I used this XmlSchemaSet to validate the xml as follows:

public void ValidateSchema(byte[] xml)
{
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
    settings.ValidationType = ValidationType.Schema;
    settings.DtdProcessing = DtdProcessing.Parse;

    // need to add dtd first. it should not be added to the schema set above
    using (XmlReader dtd = XmlReader.Create(dtdPath, settings))
    {
        settings.Schemas.Add(null, dtd);
    }

    settings.DtdProcessing = DtdProcessing.Prohibit;
    settings.Schemas.Add(this.SchemaSet); // add the schema set

    using (MemoryStream memoryStream = new MemoryStream(xml, false))
    {
        using (XmlReader validator = XmlReader.Create(memoryStream, settings))
        {
            while (validator.Read());
        }
    }
}

Notes: dtdPath, xsdPath are valid, input xml is valid, xsd files are valid

The error is: The'http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader:StandardBusinessDocument' element is not declared.


Solution

  • I created a XmlReaderSettings with following options:

    XmlReaderSettings settings = new XmlReaderSettings();
    settings.XmlResolver = new XmlXsdResolver();     // Need this for resolving include and import
    settings.ValidationType = ValidationType.Schema; // This might not be needed, I am using same settings to validate the input xml
    settings.DtdProcessing = DtdProcessing.Parse;    // I have an include that is dtd. maybe I should prohibit dtd after I compile the xsd files.
    

    Then I used it with an XmlReader to read the xsd. The important part is that I had to put a basePath so that the XmlXsdResolve can find other xsd files.

    using (XmlReader xsd = XmlReader.Create(new FileStream(xsdPath, FileMode.Open, FileAccess.Read), settings, basePath))
    {
        settings.Schemas.Add(null, xsd);
    }
    
    settings.Schemas.Compile();
    

    This is the XmlXsdResolver to find included and imported xsd files:

    protected class XmlXsdResolver : XmlUrlResolver
    {
        public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
        {
            return base.GetEntity(absoluteUri, role, ofObjectToReturn);
        }
    }