473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Resolving XML Schema documents on-the-fly?

Has anyone successfully been able to use an XmlValidatingRe ader (Framework
1.1) to validate documents against XML Schemas that haven't been added to
the XmlValidatingRe ader.Schemas collection first?

e.g. I have the following XML document, and I want to validate it.

<doc xmlns="my-first-namspace">
<node/>
<another xmlns="my-second-namespace"/>
</doc>

So I create an XmlValidatingRe ader and instruct it to use a custom
XmlResolver.

XmlTextReader tr = new XmlTextReader(f ullyQualifiedPa th);
XmlValidatingRe ader vr = new XmlValidatingRe ader(tr);
vr.ValidationTy pe = ValidationType. Schema;
vr.XmlResolver = new MyXmlResolver() ;
vr.ValidationEv entHandler += new ValidationEvent Handler(Validat ionEvent);

I then try to load the document, which causes it to be validated.

XPathDocument doc = new XPathDocument(v r);

At this point, my MyXmlResolver.R esolveUri() is called with
fullyQualifiedP ath as the relativeUri parameter, and the System.Uri I return
is then passed to MyXmlResolver.G etEntity(). So far so good.

But the next thing that happens is my ValidationEvent () method gets called
with an XmlSeverityType .Warning message: "Could not find schema information
for the element 'my-first-namespace:doc'. [...]".

I was expecting MyXmlResolver to be used to locate an XML Schema for
my-first-namespace.

If I modify my instance document to provide a schema location hint...

<doc xmlns="my-first-namspace" xsi:schemaLocat ion="my-first-namespace
mfns.xsd">
<node/>
<another xmlns="my-second-namespace"/>
</doc>

....the MyXmlResolver is called to resolve mfns.xsd.

But I can't rely on the instance document having an xsi:schemaLocat ion hint,
so is there any other way I can get XmlValidatingRe ader to ask me where to
get an XML Schema from when it comes across a new namespace?

I can't add the schema to the XmlValidatingRe ader.Schemas collection because
I don't know which schemas the instance document needs to be validated
against until the instance document is loaded. (I guess I could load the
document twice, once with validation and once without, but I was hoping to
achieve everything in one hit, as it were.)

Thanks,
- Lee
Nov 12 '05 #1
1 1650
Lee Chapman wrote:
I was expecting MyXmlResolver to be used to locate an XML Schema for
my-first-namespace.
Xmlresolver is supposed to resolve resources by URI, not by namespace,
so no URI - no call to resolver.
But I can't rely on the instance document having an xsi:schemaLocat ion hint,
so is there any other way I can get XmlValidatingRe ader to ask me where to
get an XML Schema from when it comes across a new namespace? Correct me if I'm wrong, but I believe there is no way of doing so. Also
XmlSchemaCollec tion class is sealed, so you cannoty extend it with such
behaviour.
I can't add the schema to the XmlValidatingRe ader.Schemas collection because
I don't know which schemas the instance document needs to be validated
against until the instance document is loaded. (I guess I could load the
document twice, once with validation and once without, but I was hoping to
achieve everything in one hit, as it were.)

Well, if you can try to peek into XML document before validating using
XmlTextReader (just open document and read till doument element, figure
out namespace and then close reader).
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1520
by: trek | last post by:
Good evening, I am trying to locate a standard (commonly accepted) XML schema for managing documents. What want to know is if someone has already created an XML schema for describing the pertinent data about a journal article (title, author(s), journal name, publication date, etc). If anyone can help me out here, I'd really appreciate...
0
1153
by: tsolbjor | last post by:
Is there a schema available for ethical codes? Im thinking of a code of conduct document that should be easy to update and roll out to different platforms and devices. I might use some standard text document schema, but were might I find something like that? Im new to schemas and xml documents, so I would apreciate some help :-)
0
1047
by: tsolbjor | last post by:
What program is suitable for making schema instance documents? I have tried Word, but I feel that there must be some other tools that are better than that. -- thomas
1
1016
by: Ian Pilcher | last post by:
I am using an XML-based format to define my applications data types. For example, here are the "primitive" types that holds a Java Integers and Floats: <type name="int" class="Value"> <parameters> <parameter name="valueClass" value="java.lang.Integer"/> </parameters> </type>
1
1870
by: comic_rage | last post by:
Hi, I am trying to add an annotation with a documentation, which contains several nodes. So far, I having a problem on how to code this with C#. Any help is appreciated. <xsd:annotation> <xsd:documentation> <Description>my description</Description>
8
2694
by: pradeepsarathy | last post by:
Hi all, Does the SAX parser has eventhandlers for parsing xml schema. Can we parse the xml schema the same way as we parse the xml document using SAX Parser. Thanks in advance. -pradeep
6
1666
by: Lord0 | last post by:
Hi there, How do I define in a schema that an element (<element>) may have any content i.e. text, other elements, partial elements, angle brackets etc? So all of the following would be valid: <element> aaaaaaa<foo/>bbbbbb<bar/>cccccc
5
3637
by: markus.meier | last post by:
Hi, I'm searching a tool to generate XML files based on an existing XML schema file. Does somebody know a free tool that supports this feature? Thanks Markus
3
32194
by: --CELKO-- | last post by:
Is there a simple way to get a COUNT(*) for all tables in a schema in DB2 LUW 9.0?
2
1402
by: Peter | last post by:
I'm looking for a freely available tool that can generate xml instance documents from an xml schema. I'm aware of Sun's xmlgen, but I cannot find a valid web link to it. Does anyone have a current link to Sun's xmlgen or know of a similar tool? Thank you, Peter
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.