|
|
|
start date: Tue, 21 Aug 2007 07:17:24 -0000,
posted on: microsoft.public.dotnet.xml
back
| Thread Index |
|
1
eliasen
|
|
2
Martin Honnen
|
|
3
eliasen
|
|
4
Martin Honnen
|
|
5
Martin Honnen
|
|
6
eliasen
|
Check for well formedness and get all errors
Hi
I would like to run through an XML file using C# 2.0 and check for
well formedness - and I would like to get all errors and not jsut the
first one.
My code is, off course, very simple:
XmlTextReader xtr = new XmlTextReader(xmlInstanceTextBox.Text);
try
{
while (xtr.Read())
{}
}
catch (Exception e)
{
errors += e.Message;
}
finally
{
xtr.Close();
}
When doing a validating reader, I can get an event everytime an
exception occurs, and keep running. But this one... it seems that
there is now way?
Any thouhgts?
Thanks in advance!
Date:Tue, 21 Aug 2007 07:17:24 -0000
Author:
|
Re: Check for well formedness and get all errors
eliasen wrote:
> I would like to run through an XML file using C# 2.0 and check for
> well formedness - and I would like to get all errors and not jsut the
> first one.
I think a well-formedness violation is a fatal error, XmlReader does not
allow you to continue parsing.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Date:Tue, 21 Aug 2007 14:32:19 +0200
Author:
|
Re: Check for well formedness and get all errors
> > I would like to run through an XML file using C# 2.0 and check for
> > well formedness - and I would like to get all errors and not jsut the
> > first one.
>
> I think a well-formedness violation is a fatal error, XmlReader does not
> allow you to continue parsing.
Bugger! :-) But thanks.
In .NET 1.1 I created a small program that used the
XmlValidatingReader. This could continue after a validation error had
occurred and I could get all the errors. In .NET 2.0 I created this
code and I can't seem to get all the errors:
using (FileStream fs = File.Open(xmlInstanceTextBox.Text,
FileMode.Open, FileAccess.Read))
{
//GS - Create an xml document to hold our xml
XmlDocument xdoc = new XmlDocument();
//GS - Create a reader settings, add the schema, set
for
//schema validation and add a validation event handler
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, xmlSchemaTextBox.Text);
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags =
XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationEventHandler += new
ValidationEventHandler(settings_ValidationEventHandler);
//GS - Load and validate the xml
XmlReader reader = XmlReader.Create(fs, settings);
xdoc.Load(reader);
//GS - Close the file stream when we're done
fs.Close();
}
and then off course an eventhandler that appends the error to a list
of errors. Do you know how I can get all validation errors using .NET
2.0? (Yes, I can use the XmlValidatingReader, but it would be nice to
not use deprecated classes :-) )
Thanks!
Date:Tue, 21 Aug 2007 13:06:26 -0000
Author:
|
Re: Check for well formedness and get all errors
eliasen wrote:
> using (FileStream fs = File.Open(xmlInstanceTextBox.Text,
> FileMode.Open, FileAccess.Read))
> {
> //GS - Create an xml document to hold our xml
> XmlDocument xdoc = new XmlDocument();
>
> //GS - Create a reader settings, add the schema, set
> for
> //schema validation and add a validation event handler
> XmlReaderSettings settings = new XmlReaderSettings();
> settings.Schemas.Add(null, xmlSchemaTextBox.Text);
> settings.ValidationType = ValidationType.Schema;
> settings.ValidationFlags =
> XmlSchemaValidationFlags.ProcessSchemaLocation;
> settings.ValidationEventHandler += new
> ValidationEventHandler(settings_ValidationEventHandler);
>
> //GS - Load and validate the xml
> XmlReader reader = XmlReader.Create(fs, settings);
> xdoc.Load(reader);
>
> //GS - Close the file stream when we're done
> fs.Close();
> }
>
> and then off course an eventhandler that appends the error to a list
> of errors. Do you know how I can get all validation errors using .NET
> 2.0?
Your code should do, only you need to make sure that your use the
bitwise or operator '|' on the flags e.g.
settings.ValidationFlags |=
XmlSchemaValidationFlags.ProcessSchemaLocation;
If you still have problems then tell us exactly what is happening,
whether you get an exception, which one exactly.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Date:Tue, 21 Aug 2007 15:40:44 +0200
Author:
|
Re: Check for well formedness and get all errors
On Aug 21, 3:40 pm, Martin Honnen wrote:
> Your code should do, only you need to make sure that your use the
> bitwise or operator '|' on the flags e.g.
Well, apparently, the code works. I tested it with two different
required elements missing... and only the first error appeared. But
when I test it with invalid content in two different elements, it
works well.
Probably because the missing element is a fatal error as well?
Thanks!
--
eliasen
Date:Tue, 21 Aug 2007 13:56:49 -0000
Author:
|
Re: Check for well formedness and get all errors
eliasen wrote:
> Well, apparently, the code works. I tested it with two different
> required elements missing... and only the first error appeared. But
> when I test it with invalid content in two different elements, it
> works well.
>
> Probably because the missing element is a fatal error as well?
I don't think it is a fatal error in the sense that parsing is aborted.
It might simply be that the parser has difficulties to find the right
position in the source and the schema to continue validation.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Date:Tue, 21 Aug 2007 16:07:10 +0200
Author:
|
|
|