Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xmldocument and nested schemas

Using c# and .net 3.5 I'm trying to validate an xml document against a schema that has includes.

The schemas and there includes are as below

Schema1.xsd -> include another.xsd

another.xsd -> include base.xsd

When i try to add the Schema1.xsd to the XmlDocument i get the following error.

Type 'YesNoType' is not declared or is not a simple type.

I believe i'm getting this error because the base.xsd file is not being included when i load the Schema1.xsd schema.

I'm trying to use the XmlSchemaSet class and I'm setting the XmlResolver uri to the location of the schemas.

NOTE : All schemas live under the same directory E:\Dev\Main\XmlSchemas

Here is the code

string schemaPath = "E:\\Dev\\Main\\XmlSchemas";

XmlDocument xmlDocSchema = new XmlDocument();

XmlSchemaSet s = new XmlSchemaSet();

XmlUrlResolver resolver = new XmlUrlResolver();

Uri baseUri = new Uri(schemaPath);

resolver.ResolveUri(null, schemaPath);

s.XmlResolver = resolver;

s.Add(null, XmlReader.Create(new System.IO.StreamReader(schemaPath + "\\Schema1.xsd"), new XmlReaderSettings { ValidationType = ValidationType.Schema, XmlResolver = resolver }, new Uri(schemaPath).ToString()));


xmlDocSchema.Schemas.Add(s);

ValidationEventHandler valEventHandler = new ValidationEventHandler
(ValidateNinoDobEvent);

try
{
xmlDocSchema.LoadXml(xml);
xmlDocSchema.Validate(valEventHandler);
}
catch (XmlSchemaValidationException xmlValidationError)
{
// need to interogate the Validation Exception, for possible further 
// processing.
string message = xmlValidationError.Message;
return false;
}

Can anyone point me in the right direction regarding validating an xmldocument against a schema with nested includes.

like image 250
Stuart Avatar asked Nov 14 '22 04:11

Stuart


1 Answers

I also have a nested schema case and I don't find any error in validating.My code looks like follwoing.

private string strLogger = null;
    public bool ValidateXml(string path2XMLFile, string path2XSDFile)
    {
        bool isValidFile = false;
        try
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas.Add(null, path2XSDFile);
            settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);
            XmlReader reader = XmlReader.Create(path2XMLFile, settings);
            while (reader.Read()) ;
            if (String.IsNullOrEmpty(strLogger))
            {
                isValidFile = true;
            }                
        }
        catch (Exception ex)
        {
            LoggingHandler.Log(ex);
        }
        return isValidFile;
    }
    private void settings_ValidationEventHandler(object sender, ValidationEventArgs e)
    {
        strLogger += System.Environment.NewLine + "Validation Error Message  = [" + e.Message + "], " + "Validation Error Severity = [" + e.Severity + "], " + System.Environment.NewLine;
    } 
like image 128
Kamran Shahid Avatar answered Dec 04 '22 11:12

Kamran Shahid