By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,196 Members | 1,654 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,196 IT Pros & Developers. It's quick & easy.

XML Validation using XSD File

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.