Re: XmlValidatingReader - just the FAQS?
From: Derek Harmon (loresayer_at_msn.com)
Date: 09/04/04
- Next message: Derek Harmon: "Re: XML is UTF-16 encoding but I want UTF-8 encoding ?"
- Previous message: Steven Cheng[MSFT]: "RE: How-To Dynamically Create Nested DataTables"
- In reply to: Nuevo Registrado: "XmlValidatingReader - just the FAQS?"
- Next in thread: Nuevo Registrado: "Re: XmlValidatingReader - just the FAQS?"
- Reply: Nuevo Registrado: "Re: XmlValidatingReader - just the FAQS?"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 3 Sep 2004 23:23:32 -0400
"Nuevo Registrado" <nuevo@registrado.com> wrote in message news:Xns9559790CC9C6nuevoregi@207.46.248.16...
> read an xml file and an xsd file and validate the xml file using
> the xsd without using a namespace for the schema.
The XML schema is fine, and your instance document is valid by it,
so the problem is not with these.
> Validation Error: The 'HeadCount' element is not declared. An error
> occurred at file:///d:/xml/config.xml, (2, 2).
> Validation Error: The 'Name' element is not declared. An error occurred
> at file:///d:/xml/config.xml, (3, 4).
> Validation Error: The 'Name' element is not declared. An error occurred
> at file:///d:/xml/config.xml, (4, 4).
Anytime I get a validation error for every possible piece of XML
in my instance document, I ask myself a question: did I really
load an XML schema, at all?
Because I have to figure that if I'm validating an instance
document against an XML schema without any declarations (i.e.,
an empty one), these are precisely the sorts of validation
errors I'd expect.
> ----------doit.cs
Starsky & Hutch fan?
> XmlSchemaCollection sc = new XmlSchemaCollection();
> sc.ValidationEventHandler += new ValidationEventHandler
> (ValidationCallBack);
> sc.Add(null, strXsd);
These lines _would_ work (and you're right on, passing null
to indicate that the schema targets no namespace), except
that the XmlSchemaCollection, sc, is entirely disconnected
from any of the other variables found in this method.
> XmlSchema schema = new XmlSchema();
The XmlSchema constructor should only appear in applications
that are directly programming the Schema Object Model (SOM).
Whenever this line appears in an application that is just
validating an instance document against a ready-made schema,
it's got to raise a brightly colored -- if not red -- flag.
> schema.SourceUri = strXsd;
It's probably a common misconception that setting SourceUri
causes a schema to be loaded; this is not actually what
happens.
The SourceUri property exists so that an XmlSchema can be
told what it's base path and resource are. As far as the
XmlSchema knows, it's been created from some nameless stream
of characters and only the application can give it a meaning-
ful context for it to use as it's Base URI. That's what the
SourceUri property is meant for.
> validatingReader.Schemas.Add(schema);
Adds what I'm afraid is an absolutely empty XmlSchema to the
XmlSchemaCollection of this XmlValidatingReader. :-(
OK, there are 2 ways to resolve this. The first goes with
your winning strategy at the very top of the method, but then
instead of adding an XmlSchema to an XmlSchemaCollection that
isn't connected to anything else -- add it to the XmlSchema-
Collection on your XmlValidatingReader.
validatingReader.Schemas.Add( null, strXsd);
If you still want to access the XmlSchema object you've just
added, that's possible too, since Add() returns the newly
created XmlSchema object.
XmlSchema schema = validatingReader.Schemas.Add( null, strXsd);
schema.SourceUri = strXsd;
The second solution is to use the static Read() method of
XmlSchema. Nine times out of 10, this is always what you
want to do instead of using the XmlSchema() constructor
when you're validating an instance document against an
existing XML schema document.
XmlSchema schema = XmlSchema.Read( new XmlTextWriter( strXsd), null);
schema.SourceUri = strXsd;
validatingReader.Schemas.Add( schema);
The null argument I passed to Read() is an optional Validation-
EventHandler that allows you to receive notifications of any
warnings or errors caused while loading the XML schema document.
Either of these approaches should allow you to see that the
schema document validates your instance document without any
validation errors. :-)
Derek Harmon
- Next message: Derek Harmon: "Re: XML is UTF-16 encoding but I want UTF-8 encoding ?"
- Previous message: Steven Cheng[MSFT]: "RE: How-To Dynamically Create Nested DataTables"
- In reply to: Nuevo Registrado: "XmlValidatingReader - just the FAQS?"
- Next in thread: Nuevo Registrado: "Re: XmlValidatingReader - just the FAQS?"
- Reply: Nuevo Registrado: "Re: XmlValidatingReader - just the FAQS?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|