The XmlValidatingRe ader seems to have changed drastically in .Net
Frameworks 1.0 SP3 and beyond with regards to validating XML files that
are XML Schemas.
Consider the following code:
---
class ValidatingSchem a
{
[STAThread]
static void Main(string[] args)
{
string xmlData = @"<xs:schema mlns:xs='http://www.w3.org/2001/XMLSchema' >
<xs:element name='root' />
<xs:cement id='foo'/>
</xs:schema>";
XmlTextReader reader = new XmlTextReader(n ew StringReader(xm lData));
XmlValidatingRe ader vreader = new XmlValidatingRe ader(reader);
vreader.Validat ionType = ValidationType. Schema;
vreader.Validat ionEventHandler += new ValidationEvent Handler(PrintEv ent);
while(vreader.R ead())
{}
vreader.Close() ;
}
private static void PrintEvent(obje ct source, ValidationEvent Args info)
{
Console.WriteLi ne(info.Message );
}
}
---
If I compile and run this under .Net 1.0.3705.0 I get the following output.
===
The 'http://www.w3.org/2001/XMLSchema:cemen t' element is not supported
in this context. An error occurred at (3, 7).
===
This tells me that the validation is checking my input file against some
internal knowledge of what an XML Schema can contain. After I apply .Net
FW 1.0 SP3 I get the following output from the same code:
===
Could not find schema information for the element
'http://www.w3.org/2001/XMLSchema:schem a'. An error occurred at (1, 2).
Could not find schema information for the element
'http://www.w3.org/2001/XMLSchema:eleme nt'. An error occurred at (2, 7).
Could not find schema information for the attribute 'name'. An error
occurred at (2, 18).
Could not find schema information for the element
'http://www.w3.org/2001/XMLSchema:cemen t'. An error occurred at (3, 7).
Could not find schema information for the attribute 'id'. An error
occurred at (3, 17).
===
It appears that the internal knowledge about XML Schema is gone?
When I compile and run this under .Net framework version 1.1 I get the
following (different) output:
===
Could not find schema information for the element
'http://www.w3.org/2001/XMLSchema:eleme nt'. An error occurred at , (2, 7).
Could not find schema information for the attribute 'name'. An error
occurred at , (2, 18).
Could not find schema information for the element
'http://www.w3.org/2001/XMLSchema:cemen t'. An error occurred at , (3, 7).
Could not find schema information for the attribute 'id'. An error
occurred at , (3, 17).
===
Is this change a bug? Or is it how XmlValidatingSc hema is supposed to
work? Why did the behaviour change so much in a service pack?
Ultimately, I want to be able to valid XML Schemas that contain elements
from several different namespaces against their respective schemas.
XmlValidatingRe ader seems like the tool to use, but it doesn't work
consistantly across versions. Is there another better way?
Thanks,
Harold Putman