HighTechTalks DotNet Forums  

Check for well formedness and get all errors

Dotnet XML microsoft.public.dotnet.xml


Discuss Check for well formedness and get all errors in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
eliasen
 
Posts: n/a

Default Check for well formedness and get all errors - 08-21-2007 , 03:17 AM






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!


Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 08:32 AM






eliasen wrote:

Quote:
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/


Reply With Quote
  #3  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 08:32 AM



eliasen wrote:

Quote:
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/


Reply With Quote
  #4  
Old   
eliasen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 09:06 AM



Quote:
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_ValidationEventHan dler);

//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!



Reply With Quote
  #5  
Old   
eliasen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 09:06 AM



Quote:
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_ValidationEventHan dler);

//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!



Reply With Quote
  #6  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 09:40 AM



eliasen wrote:

Quote:
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_ValidationEventHan dler);

//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/


Reply With Quote
  #7  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 09:40 AM



eliasen wrote:

Quote:
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_ValidationEventHan dler);

//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/


Reply With Quote
  #8  
Old   
eliasen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 09:56 AM



On Aug 21, 3:40 pm, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
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



Reply With Quote
  #9  
Old   
eliasen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 09:56 AM



On Aug 21, 3:40 pm, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
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



Reply With Quote
  #10  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Check for well formedness and get all errors - 08-21-2007 , 10:07 AM



eliasen wrote:

Quote:
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/


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.