Home All Groups Group Topic Archive Search About

Catching Line Numbers with XmlDocument.Validate()

Author
20 Apr 2006 9:15 PM
Brian Cobb
I have this code:

XmlDocument x = new XmlDocument();
x.Schemas.Add(
     null  //use the targetnamespace stored in the schema file
   , ExtendedTreeViewSchema  //URL of schema file.
);

x.LoadXml(XmlSource);

x.Validate(new ValidationEventHandler(Handler));

The event handler looks like this:

private static void Handler(object sender, ValidationEventArgs e)
{

        if (e.Severity == XmlSeverityType.Error || e.Severity ==
XmlSeverityType.Warning)
          System.Diagnostics.Trace.WriteLine(
            String.Format("Line: {0}, Position: {1} \"{2}\"",
                e.Exception.LineNumber, e.Exception.LinePosition,
e.Exception.Message));

}

In VS2005 IDE I can take an XML document and purposely invalidate a couple
of lines according to the schema specification.  Having done so, I see  the
following messages in the VS2005 IDE:

Warning    1    The 'columnValue' element is invalid - The value 'AUD'' is invalid
according to its datatype
VS also tells me this warning occurred on Line 31    at Column 30

Warning    2    The 'columnValue' element is invalid - The value 'CRC*'' is
invalid according to its datatype
VS also tells me that this warning occurred on Line 41    at column 31.   

Both Warnings and Lines/Column numbers are what I expect based on the errors
I purposely made in the file.


When I run the code using the same XML file,  I get the same messages but
the Line and Column numbers are always 0:

Line: 0, Position: 0 "The 'columnValue' element is invalid - The value
'AUD'' is invalid according to its datatype

Line: 0, Position: 0 "The 'columnValue' element is invalid - The value
'CRC*'' is invalid according to its datatype

What am I doing wrong?

Author
23 Apr 2006 9:45 AM
Joerg Jooss
Thus wrote Brian,

Show quote
> I have this code:
>
> XmlDocument x = new XmlDocument();
> x.Schemas.Add(
> null  //use the targetnamespace stored in the schema file
> , ExtendedTreeViewSchema  //URL of schema file.
> );
> x.LoadXml(XmlSource);
>
> x.Validate(new ValidationEventHandler(Handler));
>
> The event handler looks like this:
>
> private static void Handler(object sender, ValidationEventArgs e)
> {
> if (e.Severity == XmlSeverityType.Error || e.Severity ==
> XmlSeverityType.Warning)
> System.Diagnostics.Trace.WriteLine(
> String.Format("Line: {0}, Position: {1} \"{2}\"",
> e.Exception.LineNumber, e.Exception.LinePosition,
> e.Exception.Message));
> }
>
> In VS2005 IDE I can take an XML document and purposely invalidate a
> couple of lines according to the schema specification.  Having done
> so, I see  the following messages in the VS2005 IDE:
>
> Warning    1    The 'columnValue' element is invalid - The value 'AUD'' is
> invalid
> according to its datatype
> VS also tells me this warning occurred on Line 31    at Column 30
> Warning    2    The 'columnValue' element is invalid - The value 'CRC*'' is
> invalid according to its datatype VS also tells me that this warning
> occurred on Line 41    at column 31.
>
> Both Warnings and Lines/Column numbers are what I expect based on the
> errors I purposely made in the file.
>
> When I run the code using the same XML file,  I get the same messages
> but the Line and Column numbers are always 0:
>
> Line: 0, Position: 0 "The 'columnValue' element is invalid - The value
> 'AUD'' is invalid according to its datatype
>
> Line: 0, Position: 0 "The 'columnValue' element is invalid - The value
> 'CRC*'' is invalid according to its datatype
>
> What am I doing wrong?

Validation warnings are disabled by default. What you need to do is create
an XmlReaderSettings object and enable warnings through that object.

Unfortunately, there seems to be no way to pass your own XmlReaderSettings
object to XmlDocument.Validate(). Instead, you can use a validating XmlReader
and an  XmlNodeReader to validate an existing XmlDocument like this:

// Configure validation options
XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationEventHandler += OnValidationEvent;  // Your callback...
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags =
  XmlSchemaValidationFlags.ReportValidationWarnings |
  XmlSchemaValidationFlags.ProcessIdentityConstraints |
  XmlSchemaValidationFlags.ProcessInlineSchema |
  XmlSchemaValidationFlags.ProcessSchemaLocation;

// Wrap document in an XmlNodeReader and run validation on top of that
XmlNodeReader nodeReader = new XmlNodeReader(xmlDocument);
using(XmlReader validatingReader = XmlReader.Create(nodeReader, settings)) {
  while(validatingReader.Read()) { /* just loop through document */ } 
}

Cheers,
--
Joerg Jooss
news-re***@joergjooss.de
Author
24 Apr 2006 3:47 PM
Brian Cobb
Still getting Line number zero, position zero in the Handler.  Here is the
relevant code:

XmlDocument x = new XmlDocument();
x.LoadXml(XmlSource);

XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;     
settings.ValidationEventHandler += Handler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, ExtendedTreeViewSchema);
settings.ValidationFlags =
     XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation ;


XmlNodeReader nodeReader = new XmlNodeReader(x);
using (XmlReader validatingReader = XmlReader.Create(nodeReader, settings))
{
        while (validatingReader.Read()) { /* just loop through document */ }
}

And the handler:

private static void Handler(object sender, ValidationEventArgs e)
{

        if (e.Severity == XmlSeverityType.Error || e.Severity ==
XmlSeverityType.Warning)
          System.Diagnostics.Trace.WriteLine(
            String.Format("Line: {0}, Position: {1} \"{2}\"",
                e.Exception.LineNumber, e.Exception.LinePosition,
e.Exception.Message));

}

As before, I see the expected error messages but not the line numbers.

Show quote
"Joerg Jooss" wrote:

> Thus wrote Brian,
>
> > I have this code:
> >
> > XmlDocument x = new XmlDocument();
> > x.Schemas.Add(
> > null  //use the targetnamespace stored in the schema file
> > , ExtendedTreeViewSchema  //URL of schema file.
> > );
> > x.LoadXml(XmlSource);
> >
> > x.Validate(new ValidationEventHandler(Handler));
> >
> > The event handler looks like this:
> >
> > private static void Handler(object sender, ValidationEventArgs e)
> > {
> > if (e.Severity == XmlSeverityType.Error || e.Severity ==
> > XmlSeverityType.Warning)
> > System.Diagnostics.Trace.WriteLine(
> > String.Format("Line: {0}, Position: {1} \"{2}\"",
> > e.Exception.LineNumber, e.Exception.LinePosition,
> > e.Exception.Message));
> > }
> >
> > In VS2005 IDE I can take an XML document and purposely invalidate a
> > couple of lines according to the schema specification.  Having done
> > so, I see  the following messages in the VS2005 IDE:
> >
> > Warning    1    The 'columnValue' element is invalid - The value 'AUD'' is
> > invalid
> > according to its datatype
> > VS also tells me this warning occurred on Line 31    at Column 30
> > Warning    2    The 'columnValue' element is invalid - The value 'CRC*'' is
> > invalid according to its datatype VS also tells me that this warning
> > occurred on Line 41    at column 31.
> >
> > Both Warnings and Lines/Column numbers are what I expect based on the
> > errors I purposely made in the file.
> >
> > When I run the code using the same XML file,  I get the same messages
> > but the Line and Column numbers are always 0:
> >
> > Line: 0, Position: 0 "The 'columnValue' element is invalid - The value
> > 'AUD'' is invalid according to its datatype
> >
> > Line: 0, Position: 0 "The 'columnValue' element is invalid - The value
> > 'CRC*'' is invalid according to its datatype
> >
> > What am I doing wrong?
>
> Validation warnings are disabled by default. What you need to do is create
> an XmlReaderSettings object and enable warnings through that object.
>
> Unfortunately, there seems to be no way to pass your own XmlReaderSettings
> object to XmlDocument.Validate(). Instead, you can use a validating XmlReader
> and an  XmlNodeReader to validate an existing XmlDocument like this:
>
> // Configure validation options
> XmlReaderSettings settings = new XmlReaderSettings();
> settings.CloseInput = true;
> settings.ValidationEventHandler += OnValidationEvent;  // Your callback...
> settings.ValidationType = ValidationType.Schema;
> settings.ValidationFlags =
>   XmlSchemaValidationFlags.ReportValidationWarnings |
>   XmlSchemaValidationFlags.ProcessIdentityConstraints |
>   XmlSchemaValidationFlags.ProcessInlineSchema |
>   XmlSchemaValidationFlags.ProcessSchemaLocation;
>
> // Wrap document in an XmlNodeReader and run validation on top of that
> XmlNodeReader nodeReader = new XmlNodeReader(xmlDocument);
> using(XmlReader validatingReader = XmlReader.Create(nodeReader, settings)) {
>   while(validatingReader.Read()) { /* just loop through document */ } 
> }
>
> Cheers,
> --
> Joerg Jooss
> news-re***@joergjooss.de
>
>
>
Author
24 Apr 2006 6:21 PM
Brian Cobb
It works when I do away with the XmlDocument and replace XmlNodeReader with
StringReader, i.e.,

StringReader r = new StringReader(XmlSource);

using (XmlReader validatingReader = XmlReader.Create(r, settings)) {
        while (validatingReader.Read()) { /* just loop through document */ }
}





Show quote
"Brian Cobb" wrote:

> Still getting Line number zero, position zero in the Handler.  Here is the
> relevant code:
>
> XmlDocument x = new XmlDocument();
> x.LoadXml(XmlSource);
>
> XmlReaderSettings settings = new XmlReaderSettings();
> settings.CloseInput = true;     
> settings.ValidationEventHandler += Handler;
>
> settings.ValidationType = ValidationType.Schema;
> settings.Schemas.Add(null, ExtendedTreeViewSchema);
> settings.ValidationFlags =
>      XmlSchemaValidationFlags.ReportValidationWarnings |
> XmlSchemaValidationFlags.ProcessIdentityConstraints |
> XmlSchemaValidationFlags.ProcessInlineSchema |
> XmlSchemaValidationFlags.ProcessSchemaLocation ;
>
>
> XmlNodeReader nodeReader = new XmlNodeReader(x);
> using (XmlReader validatingReader = XmlReader.Create(nodeReader, settings))
> {
>         while (validatingReader.Read()) { /* just loop through document */ }
> }
>
> And the handler:
>
> private static void Handler(object sender, ValidationEventArgs e)
> {
>
>         if (e.Severity == XmlSeverityType.Error || e.Severity ==
> XmlSeverityType.Warning)
>           System.Diagnostics.Trace.WriteLine(
>             String.Format("Line: {0}, Position: {1} \"{2}\"",
>                 e.Exception.LineNumber, e.Exception.LinePosition,
> e.Exception.Message));
>        
> }
>
> As before, I see the expected error messages but not the line numbers.
>
> "Joerg Jooss" wrote:
>
> > Thus wrote Brian,
> >
> > > I have this code:
> > >
> > > XmlDocument x = new XmlDocument();
> > > x.Schemas.Add(
> > > null  //use the targetnamespace stored in the schema file
> > > , ExtendedTreeViewSchema  //URL of schema file.
> > > );
> > > x.LoadXml(XmlSource);
> > >
> > > x.Validate(new ValidationEventHandler(Handler));
> > >
> > > The event handler looks like this:
> > >
> > > private static void Handler(object sender, ValidationEventArgs e)
> > > {
> > > if (e.Severity == XmlSeverityType.Error || e.Severity ==
> > > XmlSeverityType.Warning)
> > > System.Diagnostics.Trace.WriteLine(
> > > String.Format("Line: {0}, Position: {1} \"{2}\"",
> > > e.Exception.LineNumber, e.Exception.LinePosition,
> > > e.Exception.Message));
> > > }
> > >
> > > In VS2005 IDE I can take an XML document and purposely invalidate a
> > > couple of lines according to the schema specification.  Having done
> > > so, I see  the following messages in the VS2005 IDE:
> > >
> > > Warning    1    The 'columnValue' element is invalid - The value 'AUD'' is
> > > invalid
> > > according to its datatype
> > > VS also tells me this warning occurred on Line 31    at Column 30
> > > Warning    2    The 'columnValue' element is invalid - The value 'CRC*'' is
> > > invalid according to its datatype VS also tells me that this warning
> > > occurred on Line 41    at column 31.
> > >
> > > Both Warnings and Lines/Column numbers are what I expect based on the
> > > errors I purposely made in the file.
> > >
> > > When I run the code using the same XML file,  I get the same messages
> > > but the Line and Column numbers are always 0:
> > >
> > > Line: 0, Position: 0 "The 'columnValue' element is invalid - The value
> > > 'AUD'' is invalid according to its datatype
> > >
> > > Line: 0, Position: 0 "The 'columnValue' element is invalid - The value
> > > 'CRC*'' is invalid according to its datatype
> > >
> > > What am I doing wrong?
> >
> > Validation warnings are disabled by default. What you need to do is create
> > an XmlReaderSettings object and enable warnings through that object.
> >
> > Unfortunately, there seems to be no way to pass your own XmlReaderSettings
> > object to XmlDocument.Validate(). Instead, you can use a validating XmlReader
> > and an  XmlNodeReader to validate an existing XmlDocument like this:
> >
> > // Configure validation options
> > XmlReaderSettings settings = new XmlReaderSettings();
> > settings.CloseInput = true;
> > settings.ValidationEventHandler += OnValidationEvent;  // Your callback...
> > settings.ValidationType = ValidationType.Schema;
> > settings.ValidationFlags =
> >   XmlSchemaValidationFlags.ReportValidationWarnings |
> >   XmlSchemaValidationFlags.ProcessIdentityConstraints |
> >   XmlSchemaValidationFlags.ProcessInlineSchema |
> >   XmlSchemaValidationFlags.ProcessSchemaLocation;
> >
> > // Wrap document in an XmlNodeReader and run validation on top of that
> > XmlNodeReader nodeReader = new XmlNodeReader(xmlDocument);
> > using(XmlReader validatingReader = XmlReader.Create(nodeReader, settings)) {
> >   while(validatingReader.Read()) { /* just loop through document */ } 
> > }
> >
> > Cheers,
> > --
> > Joerg Jooss
> > news-re***@joergjooss.de
> >
> >
> >

AddThis Social Bookmark Button