After two days I am at wits end, but I am too OCD'ed to give up.
Please tell me what I have got wrong because no Validation Error is
being thrown.
The element: "<datestamp>I should fail</datestamp>"
does not conform to schema. Adding the schema to the
XmlReaderSettings->Schemas field at runtime does result in error being
thrown. As I read it, this step should not be neccessary as the
ProcessSchemaLocation flag should automatically pick up the referenced
schemas in the XML file.
Note: the XML file is actually a node that will be inserted to an
existing XML file.
Below is XML file:
(
"<provenance xmlns=\"http://www.openarchives.org/OAI/
2.0/provenance\"" + "\n" +
"\t" + "xmlns:xsi=\"http://www.w3.org/2001/
XMLSchema-instance\"" + "\n" +
"\t" + "xsi:schemaLocation=\"http://
www.openarchives.org/OAI/2.0/provenance" + "\n" +
"\t" + "http://www.openarchives.org/OAI/2.0/
provenance.xsd\">" + "\n" +
"\t" + "<originDescription harvestDate=
\"2002-02-02T14:10:02Z\" altered=\"true\">" + "\n" +
"\t" + "<baseURL>http://the.oa.org</baseURL>" +
"\n" +
"\t" + "<identifier>oai:r2.org:klik001</
identifier>" + "\n" +
"\t" + "<datestamp>I should fail</datestamp>" +
"\n" +
"\t" + "<metadataNamespace>http://
www.openarchives.org/OAI/2.0/oai_dc/</metadataNamespace>" + "\n" +
"\t" + "\t" + "<originDescription harvestDate=
\"2002-01-01T11:10:01Z\" altered=\"false\">" + "\n" +
"\t" + "\t" + "\t" + "<baseURL>http://some.oa.org</
baseURL>" + "\n" +
"\t" + "\t" + "\t" +
"<identifier>oai:r2.org:klik001</identifier>" + "\n" +
"\t" + "\t" + "\t" + "<datestamp>2001-01-01</
datestamp>" + "\n" +
"\t" + "\t" + "\t" + "<metadataNamespace>http://
www.openarchives.org/OAI/2.0/oai_dc/</metadataNamespace>" + "\n" +
"\t" + "\t" + "</originDescription>" + "\n" +
"\t" + "</originDescription>" + "\n" +
"\t" + "</provenance>");
Here is XML validation class:
public class XmlValidator
{
public static bool isValid;
public static void Validate(String inFile)
{
XmlReader reader = null;
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new
ValidationEventHandler(validationEventHandler);
settings.ValidationFlags &=
XmlSchemaValidationFlags.ProcessSchemaLocation;
//settings.ValidationFlags &=
XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags &=
XmlSchemaValidationFlags.ReportValidationWarnings;
System.IO.StringReader strReader = new
System.IO.StringReader(inFile);
reader = XmlReader.Create(strReader, settings);
isValid = true;
while (reader.Read())
{
}
}
static void validationEventHandler(object sender,
ValidationEventArgs args)
{
isValid = false;
}
}
Thanks in advance for granting peace to my mind.
Jim