Validating an XML document against schema

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I am trying to validate an XML document against schema, but for unknown reasons it always validates to true. Both XML and XSD files have the proper format. But when i am using the wrong XML file it also validates :(

<div style="color:Black; background-color:White
<pre><span style="color:Green // In a method i am doing this:

<span style="color:Green // Load the file .
XPathDocument doc = <span style="color:Blue new XPathDocument(filename);

<span style="color:Green //Validate file with schema
ValidationEventHandler validationHandler = <span style="color:Blue new ValidationEventHandler(XmlValidationError);
XPathNavigator navigator = doc.CreateNavigator();
XmlSchemaSet <span style="color:Blue set = <span style="color:Blue new XmlSchemaSet();
XmlSchema schema = XmlSchema.Read(<span style="color:Blue new XmlTextReader(fileSchema), validationHandler);


<span style="color:Green // Add our schema to the XmlSchemaSet
<span style="color:Blue set.Add(schema);

<span style="color:Green // Now we can validate
<span style="color:Blue if (!navigator.CheckValidity(<span style="color:Blue set, validationHandler))
{
MessageBox.Show(<span style="color:#A31515 "Wrong structure", <span style="color:#A31515 "Eroare");
}
<span style="color:Blue else
{
MessageBox.Show(<span style="color:#A31515 "Proper structure", <span style="color:#A31515 "Eroare"); /// <--- Always here, dunno why.
}

<span style="color:Green // My handles looks like:
<span style="color:Green // Handles XML Validation errors.
<span style="color:Blue public <span style="color:Blue static <span style="color:Blue void XmlValidationError(<span style="color:Blue object sender, ValidationEventArgs e)
{
<span style="color:Blue if (e.Severity == XmlSeverityType.Warning)
{
MessageBox.Show(<span style="color:#A31515 "Warning");
}
<span style="color:Blue else <span style="color:Blue if (e.Severity == XmlSeverityType.Error)
{
MessageBox.Show(<span style="color:#A31515 "Eroare");
}
}


[/code]

Can you please give me a hint?


View the full article
 
Back
Top