Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 03:25 PM
Stefan Hoffmann
Guest
 
Posts: n/a
Default Validation ignores fixed attribute

hi @all,

I'm trying to validate a XML against a schema using the example from the
MSDN:

http://msdn.microsoft.com/en-us/libr...ationtype.aspx

In my case this seems to validates my file, because the
ValidationCallBack handler is not called.
But the file is invalid due to a wrong fixed attribute.

I don't know why, so any clue is appreciated.

My GPX (GPS Exchange Format) file:

<?xml version="1.0"?>
<gpx
version="1.0"
creator="Holux Utility"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0
http://www.topografix.com/GPX/1/0/gpx.xsd">

<wpt lat="48.342567" lon="10.865602">
<ele>137.59</ele>
<time>2008-09-03T15:17:23Z</time>
<name><![CDATA[Point 0]]></name>
</wpt>

<wpt lat="48.342548" lon="10.865611">
<ele>137.72</ele>
<time>2008-09-03T15:17:24Z</time>
<name><![CDATA[Point 1]]></name>
</wpt>

</gpx>



My code looks like this (.Net 3.5, dlgFileOpen.FileName points to the
file above):

XmlSchemaSet sc = new XmlSchemaSet();
sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd");

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);

XmlReader reader = XmlReader.Create(dlgFileOpen.FileName, settings);
while (reader.Read()) ;

mfG
--stefan <--
  #2  
Old September 4th, 2008, 03:35 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Validation ignores fixed attribute

Stefan Hoffmann wrote:
Quote:
<gpx
version="1.0"
creator="Holux Utility"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
So here the namespace URI is http://www.topografix.com/GPX/1/0
while later on you add a schema for
Quote:
XmlSchemaSet sc = new XmlSchemaSet();
sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd");
http://www.topografix.com/GPX/1/1 which is different. Make sure you add
a schema for the namespace your XML instance document uses, otherwise
validation will only emit some warnings that no matching schema is found
and your code
Quote:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
will not even so those warnings as you do not have the ValidationFlags
set on your settings object to ReportValidationWarnings.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old September 4th, 2008, 05:55 PM
Stefan Hoffmann
Guest
 
Posts: n/a
Default Re: Validation ignores fixed attribute

hi Martin,

Martin Honnen wrote:
Quote:
will not even so those warnings as you do not have the ValidationFlags
set on your settings object to ReportValidationWarnings.
Thanks.

btw, is there any idiom to detect the necessary schema for a XML file?
In my GPX case there are two schemas.

I'm considering using this for the XmlReaderSettings.Schemas:

XmlSchemaSet sc = new XmlSchemaSet();
sc.Add("http://www.topografix.com/GPX/1/0", "Resources\\gpx-1.0.xsd");
sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd");

So when the validation handler is not called, then one schema applies to
the file.

Is this correct?

mfG
--stefan <--
  #4  
Old September 4th, 2008, 06:25 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Validation ignores fixed attribute

Stefan Hoffmann wrote:
Quote:
btw, is there any idiom to detect the necessary schema for a XML file?
In my GPX case there are two schemas.
>
I'm considering using this for the XmlReaderSettings.Schemas:
>
XmlSchemaSet sc = new XmlSchemaSet();
sc.Add("http://www.topografix.com/GPX/1/0", "Resources\\gpx-1.0.xsd");
sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd");
>
So when the validation handler is not called, then one schema applies to
the file.
The validating parser will look at the namespace of the root element of
the XML document to be validated and then check for a schema with a
matching targetNamespace in its XmlSchemaSet. If it does not find a
matching schema then a warning is issued. If a matching schema is found
the parser checks whether there is a top level defintion for the root
element and validates against that definition, reporting errors as
needed to the handler. If there is no top level definition for the root
element then this is also an error. So the above should work.

Note that you can shorten the code, there is no need to explicitly
create and assing an XmlSchemaSet, you can simply call
settings.Schemas.Add. And passing in the schema URI to the Add method is
not necessary, if you pass in null for that parameter then it simply
uses the targetNamespace of the schema passed in as the second argument.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #5  
Old September 4th, 2008, 07:04 PM
Stefan Hoffmann
Guest
 
Posts: n/a
Default Re: Validation ignores fixed attribute

hi Martin,

Martin Honnen wrote:
Quote:
Quote:
>XmlSchemaSet sc = new XmlSchemaSet();
>sc.Add("http://www.topografix.com/GPX/1/0", "Resources\\gpx-1.0.xsd");
>sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd");
Note that you can shorten the code, there is no need to explicitly
create and assing an XmlSchemaSet, you can simply call
settings.Schemas.Add. And passing in the schema URI to the Add method is
not necessary, if you pass in null for that parameter then it simply
uses the targetNamespace of the schema passed in as the second argument.
Ah, cool. Thanks a lot.


mfG
--stefan <--
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles