473,387 Members | 1,569 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,387 software developers and data experts.

XML Validation using XSD File

Hello,

I have an XML and XSD file. When I do validation in the XML file I only get the first error. Is
it possible to list all errors?

One other question.

-------------Microsoft Sample Code -------------------
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
settings.ValidationType = ValidationType.Schema;

XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
document.Validate(eventHandler);
-----------------http://msdn.microsoft.com/en-us/library/ms162371.aspx ---------------
What is the point of first creating a XmlReader and passing in the XmlReaderSettings ?

Wouldn't it be easier to just use something like this:

----------------------------------------------
XmlDocument document = new XmlDocument();
document.Load(Filename)

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;

document.Schemas.Add(settings.Schemas.Add(null, "contosoBooks.xml"));
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
document.Validate(eventHandler);
----------------------------------------------

Am I missing something? What is the point of loading in the reader first.

Thank you for taking the time to read this.
--
Adhal Freeware
http://www.adhal.org

There are only 10 types of people in the world: Those who understand binary, and those who don't.
Jun 27 '08 #1
3 4041
Adhal wrote:
I have an XML and XSD file. When I do validation in the XML file I
only get the first error. Is it possible to list all errors?
If you set up an ValidationEventHandler then the validation process
should report all errors it finds. If you do not set up an
ValidationEventHandler then the fist validation error throws an exception.
If you do not get "all errors" reported then we need to look at the
details of the XML and the schema(s).
One other question.

-------------Microsoft Sample Code -------------------
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.contoso.com/books",
"contosoBooks.xsd");
settings.ValidationType = ValidationType.Schema;

XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
ValidationEventHandler eventHandler = new
ValidationEventHandler(ValidationEventHandler);
document.Validate(eventHandler);
-----------------http://msdn.microsoft.com/en-us/library/ms162371.aspx
---------------
What is the point of first creating a XmlReader and passing in the
XmlReaderSettings ?
I would rather ask what is the point of using the Validate method? The
Validate method is there to validate an in-memory DOM document after you
have performed changes with the DOM API (e.g. create and inserted nodes
or removed nodes). If you don't do that and have the XML file and the
XSD schema file(s) then you do not need to use an XmlDocument at all but
you can simply use an XmlReader with the proper XmlReaderSettings as in
the following approach:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, "schema1.xsd");
// add further schemas if needed e.g.
settings.Schemas.Add(null, "schema2.xsd");
settings.ValidationEventHandler += new
ValidationEventHandler(yourHandler);

using (XmlReader reader = XmlReader.Create("file.xml", settings))
{
while (reader.Read()) {}
}


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2

"Adhal" <us**@example.netwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
Hello,

I have an XML and XSD file. When I do validation in the XML file I only
get the first error. Is it possible to list all errors?
The code in the link using the loop and dumping errors to an array is what I
used.

http://dotnetics.integrethics.com/?p=50

Jun 27 '08 #3
Adhal wrote:
I used XmlDocument to get the first element (DocumentElement) then I
loaded the correct schema to
validate it against.

Is there a better way to do this?

I can use reader to parse through the first few elements, I guess.
I tend to use XmlDocument when I want to manipulate an existing XML
document. If needed/wanted I can then also validate the changes with the
Validate method.
If I purely want to validate an existing document then I use XmlReader.
Even if I needed to look at the root/document element first to decide
which schema to use I would use XmlReader, one which reads the root
element to decide on the schema, a second to validate against the
schema. That should still be faster and consume much less memory than
using an XmlDocument.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 28 '08 #4

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

Similar topics

13
by: Reuben Chew | last post by:
It seems that due to XP SP2's MIME type restrictions, when using Internet Explorer 6 SP2 to validate documents, it will spit out this error: ----------------------------------- Sorry, I am unable...
2
by: Sudip Chakraborty | last post by:
Is there a way to see constraint validation errors while loading xml into a DataSet ? I'm interested in the line number in the xml file which is causing the error. I've enclosed the relevant stack...
18
by: Vlad | last post by:
I have the following code: I have a local copy of the DTD that I need to validate incoming XML documents against. The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd"> define. When the...
4
by: Wayne Wengert | last post by:
I have an XML document and a corresponding xsd document I built. I want to validate the xml document against that Schema but when I use the MSDN validator it has no place to select the xsd...
2
by: Humberto Alvarez | last post by:
Hi All I'm using asp requiredfieldvalidator and regularexpressionvalidator to validate a text field and a file input field respectively. The validation messages (the text property of the...
1
by: billa1972 | last post by:
Hi, I am trying to hook into Yellow Freight's rating webservice. Below is the wsdl. When i try and create a proxy file with wsdl.exe i get the following errors, see below. Also, when i...
0
by: info | last post by:
Hi, Is it possible to include in the Schema validation file, every custom error message for each validation rules? This mean, in the same xsd file we can have the validation rules (patterns)...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
5
by: paul_zaoldyeck | last post by:
does anyone know how to validate an xml file against multiple defined schema? can you show me some examples? i'm making here an xml reader.. thank you
1
by: NamelessNumberheadMan | last post by:
I can't seem to get Struts 2 validations to work. I have been converting from Strust 1 to Struts 2. So far I've refactored all the code (for this particular module) on the back end, rewrote the jsp...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.