Re: XmlValidatingReader - just the FAQS?

From: Derek Harmon (loresayer_at_msn.com)
Date: 09/04/04


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



Relevant Pages

  • Re: validating if a file is a true valid xml file that fits a cert
    ... ''' Validates an XML stream against the specified XML Schema. ... ValidateXmlFileException if the XML stream or XML Schema fails validation. ... Public Sub ValidateXmlFile(ByVal SchemaNamespace As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: xml schema
    ... Strict adherence to an XML standard (schema) must be done ... handle schema validation errors in production. ... .NET XML schema validation classes allow one to do incremental ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trouble validating XML w/ XSD.
    ... For a root element node, if a schema matching the namespace if the element ... then lax validation kicks in according to XSD spec. ... > I've got the some code to try and validate some xml. ...
    (microsoft.public.dotnet.xml)
  • Re: What is the logic of storing XML in a Database?
    ... I thought XML was supposed to be used for transport? ... The validation ... schema describing that CSV file, for the simple reason that no such ... standard schema exists. ...
    (comp.databases.theory)
  • Re: XML Architecture Question (Best Practices?)
    ... Validation checks compliance of a given XML instance ... document to the schema it's being validated against. ... old XML Document Type Description is a schema language, ...
    (comp.text.xml)

Quantcast