XmlDocument.Validate VS XmlSchemaValidator.ValidateElement

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
Im facing a strange behavior when validating a document against a schema using two different methods.
Method 1 : using XmlDocument.Validate
Method 2 : usnig XmlSchemaValidator.ValidateElement
The two methods, for the same document and schema, doest behave the same.
I intentionally introduced an error in my document and Method 1 detect it as expect, not Method 2 !
Coud someone understand then explain me why ?
Here are sample codes to help yuo understand better my situation.
The Test Code :
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; void Validate()
{

<span style="color:Blue; string CtgNamespace = <span style="color:#A31515; "http://www.myNamespace.com";

XmlReader xReader = XmlReader.Create(<span style="color:#A31515; "Resources/Error.xml");

XmlSchemaSet schemaSet = <span style="color:Blue; new XmlSchemaSet();
schemaSet.Add(CtgNamespace, <span style="color:#A31515; "Resources/Error.xsd");

<span style="color:Green; // 1 -
<span style="color:Green; // Validation using XmlDocument
<span style="color:Green; //
XmlDocument xDoc = <span style="color:Blue; new XmlDocument();
xDoc.Load(xReader);
xDoc.Schemas = schemaSet;
xDoc.Validate((sender, e) => {
Console.WriteLine(<span style="color:#A31515; "Validation with XmlDocument " + e.Message);
});

<span style="color:Green; // 2 -
<span style="color:Green; // Validation using XmlSchemaValidator
<span style="color:Green; //
XmlNamespaceManager manager = <span style="color:Blue; new XmlNamespaceManager(xReader.NameTable);
XmlSchemaValidator validator = <span style="color:Blue; new XmlSchemaValidator(xReader.NameTable, schemaSet, manager, XmlSchemaValidationFlags.None);
validator.ValidationEventHandler += (sender, e) =>
{
Console.WriteLine(<span style="color:#A31515; "[ ERROR ] " + e.ToString());
};

<span style="color:Green; // Root
XmlSchemaInfo result = <span style="color:Blue; new XmlSchemaInfo();
<span style="color:Green; //validator.Initialize(schemaSet.GlobalElements[new XmlQualifiedName("Root", CtgNamespace)]);
validator.Initialize();

<span style="color:Green; // SHOULD RAISE ERROR
validator.ValidateElement(<span style="color:#A31515; "Root", CtgNamespace, result);

}
}
[/code]
The Schema :
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; "<span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema
<span style="color:Red; xmlns:ctg<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.myNamespace.com<span style="color:Black; "
<span style="color:Red; xmlns:xs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/2001/XMLSchema<span style="color:Black; "
<span style="color:Red; targetNamespace<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.myNamespace.com<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Root<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; ctg:Root<span style="color:Black; "<span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Child<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:string<span style="color:Black; "<span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Root<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; all<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; ref<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; ctg:Child<span style="color:Black; "<span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; all<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema<span style="color:Blue; >

[/code]
<div style="color:Black;background-color:White; <pre>
 
[/code]
The Xml document
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; ctg<span style="color:Blue; :<span style="color:#A31515; Root <span style="color:Red; xmlns:ctg<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.myNamespace.com<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ctg<span style="color:Blue; :<span style="color:#A31515; Child<span style="color:Blue; ><span style="color:Blue; </<span style="color:#A31515; ctg<span style="color:Blue; :<span style="color:#A31515; Child<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ctg<span style="color:Blue; :<span style="color:#A31515; Child<span style="color:Blue; ><span style="color:Blue; </<span style="color:#A31515; ctg<span style="color:Blue; :<span style="color:#A31515; Child<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ctg<span style="color:Blue; :<span style="color:#A31515; Root<span style="color:Blue; >
[/code]
Thanks in advance !
Riana

View the full article
 
Back
Top