473,226 Members | 1,565 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,226 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 4028
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.