XML Validation using XSD File

  • Thread starter Thread starter Adhal
  • Start date Start date
A

Adhal

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);
 
Adhal said:
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 said:
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()) {}
}

I see. I misunderstood the way Xmldocument is used. I am new to C# .NET and even newer to XML.

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.

Thanks for the reply, it cleared my misunderstandings.
 
Adhal said:
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.
 
Back
Top