473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Validating against XML Schema question.

AP
Hello,

I'm trying to use .NET to validate incoming XML documents against the
appropriate schema in our database. I do not want to use the document's
schemaLocation attribute to validate for various reasons, but rather check
against a schema that I pull from a database and add to the reader's schema
collection. I am curious as to why the XmlValidatingReader fires an event
when the document is invalid, rather than throwing an exception? This makes
developing a method to do this a lot harder. Is there some reason it has
been implemented this way?

Thanks,

Adam
Nov 12 '05 #1
3 1843
j t
My guess is this will allow you to ignore certain errors. If an exception is
thrown, you can't validate the rest of the document. Just a guess.

On a side note, I'm having trouble getting the XmlValidatingReader to work.
I've created both an xml & xsd file, and they pass validation through xml
spy. However, when I run it through the XmlValidatingReader I'm getting the
errors

'Could not find schema information for the element <myElement>'

and

'Could not find schema information for the attribute <myAttribute>'

also

'The attribute targetNamespace does not match the designated namespace URI'

for every element & attribute in the document.

why would it validate for one and not the other?

thanks,

jason

"AP" <ad***@indra.com> wrote in message
news:<ex*************@tk2msftngp13.phx.gbl>...
Hello, I'm trying to use .NET to validate incoming XML documents against the appropriate schema in our database. I do not want to use the document's schemaLocation attribute to validate for various reasons, but rather check against a schema that I pull from a database and add to the reader's schema
collection. I am curious as to why the XmlValidatingReader fires an event when the document is invalid, rather than throwing an exception? This makes
developing a method to do this a lot harder. Is there some reason it has been implemented this way? Thanks, Adam

Nov 12 '05 #2
Bob
Here is something I did in C++ that will solve your problem of it not working. Also an example of XML/XSD. This will throw the exception

FileStream *myFile = new FileStream(pNoteFilename, System::IO::FileMode::Open, System::IO::FileAccess::Read)
StreamReader *myReader = new StreamReader(myFile)
XmlTextReader *tr = new XmlTextReader(myReader)
XmlDocument *myXmlDataDocument = new XmlDocument

// Set up the Schema for validatio
strSchema.Format(TEXT("%s/Notes/notes.xsd"), theApp.LocalDir)
FileStream *mySchema = new FileStream(strSchema, System::IO::FileMode::Open, System::IO::FileAccess::Read)
StreamReader *mySchemaReader = new StreamReader(mySchema)
XmlTextReader *sr = new XmlTextReader(mySchemaReader)

XmlSchemaCollection* myXmlSchemaCollection = new XmlSchemaCollection()
tr

myXmlSchemaCollection->Add("notes.xsd" , sr)

catch (System::Exception *z

CString strTemp = z->Message->ToString()
AfxMessageBox(strTemp)
return(_FAILED_)

XmlValidatingReader *valReader = new XmlValidatingReader(tr)
valReader->ValidationType = ValidationType::Auto
valReader->Schemas->Add(myXmlSchemaCollection)

tr

myXmlDataDocument->Load(valReader)

catch(System::Exception *e

CString strError = e->Message->ToString()
AfxMessageBox(strError)
myFile->Close()
return(_FAILED_)
#### First part of schem

<?xml version="1.0" ?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema
xmlns="notes.xsd
elementFormDefault="qualified
targetNamespace="notes.xsd"

#####

First part of XM

?xml version="1.0" encoding="utf-8"?><IADSNOTES xmlns="notes.xsd" count="1">
Nov 12 '05 #3
> I am curious as to why the XmlValidatingReader fires an event
when the document is invalid, rather than throwing an exception? This makes developing a method to do this a lot harder.


It only fires an event if you set an event handler. If no event handler is
provided an XmlException is thrown on the first validation error (Severity
is equal to XmlSeverityType.Error). E.g.

Regards,
Peter Theill
Nov 12 '05 #4

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

Similar topics

2
by: Will | last post by:
I have been having problems validating an XForms document against the XForms schema located at http://www.w3.org/MarkUp/Forms/2002/XForms-Schema.xsd. I have reduced the XForm to its bare bones...
4
by: billcoumbe | last post by:
any recommendations? I'm looking for something that will just run from the unix command line to validate large (20-50Mb) XML files against an XML DTD. Ideally something that is actively...
6
by: Iain | last post by:
I've got a system which takes an XML file, translates it into an update gram and then loads it into my database with SQLXML3 (all in dot net). But it's fragile. And the SQLXML 3 error reporting...
1
by: Christian | last post by:
Hi, I load an Xml-file "customers.xml" into a DataSet (works fine) but then how do I validate it against a schema (e.g. customers.xsd) ? my customers.xml: <?xml version="1.0"...
3
by: Shailendra Batham | last post by:
hi guys I need your suggestions / opinion for doing this the right way. I have a XML and a Schema for the same What I want is when its validated against the schema, it should give custom...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
by: Ian | last post by:
I've inherited some xml that doesn't have a namespace and doesn't have a reference to the schema. The schema doesn't have a targetNamespace attribute. I'm trying to build a test tool, in C#, which...
7
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
I wrote a method to validate and xml file against a schema. If the file does not conform to the schema, it throws an error. It works fine except for one curious thing. If I try to validate an...
4
by: agda.karlberg | last post by:
Hello, I need to remove the DTD reference from an xml document, the reason for this is that we want to validate against a schema instead (which we have locally). It takes up to a minute to fetch...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.