-
Mapping validation error to DOM document
When using the .NET ValidatingReader functionality you can get an error list
of
invalid xml lines or a schema exception, according to the schema . Both of
these give you the element name and the line number and position e.g. Line
56, Column 4, MyNameSpace:LastName.
I want to be able to map this error data, in code, back to the xml being
validated. Now i cannot see a method within the .NET class libaries of
saying
something like MoveToElement(LinePos,CharPos) or indeed a way of using
XPath.
The XmlTextReader does have a LineNumber and LinePosition property but these
are RO. I could use the XmlDocument's SelectNodes to find all the occurences
of the element named in the error but the node classes returned do not
expose line or char postion info.
Does anyone have any ideas?
Thanks
Pat Long
-
Re: Mapping validation error to DOM document
It's true that there's no direct method for accomplishing what you want;
however, it shouldn't be too difficult to develop. Because you know the
element name and line number when the error occurs, by simply keeping a
count for each element, you should be able to find the element you want
based on the count when the error occurs. For example, if you have a
document like this:
<?xml version="1.0" encoding="UTF-8"?>
<people xmlns:company xmlns:emp=http://www.mycompany.com/employees
version"1.0">
<emp erson>data here</emp erson>
<emp erson>data here</emp erson>
<emp erson>data here</emp erson>
<person>data here</emp erson>
<em erson>data here</em:Person>
<emp erson>data here</emp erson>
<emp erson>data here</emp erson>
</people>
As you read the document, you'll receive a validation error on the 5th
person element. If you add each element name to a collection as the reader
encounters it, or increment the count the collection would contain:
people, 1
person, 5
For validation errors, you could load the document--without validation--find
the 5th person node with an XPath query, and then conceivably fix the
problem using DOM methods to replace the node with a valid node. For badly
formed XML errors, you'll need to write custom code to find and fix the
error by loading the XML file as a text file and parsing the data yourself,
because the XML parser won't load badly formed XML.
HTH
Russell Jones
Sr. Web Development Editor,
DevX.com
"Patrick Long" <Patrick_long@csi.com> wrote in message
news:3bb8e780@news.devx.com...
> When using the .NET ValidatingReader functionality you can get an error
list
> of
> invalid xml lines or a schema exception, according to the schema . Both of
> these give you the element name and the line number and position e.g. Line
> 56, Column 4, MyNameSpace:LastName.
>
> I want to be able to map this error data, in code, back to the xml being
> validated. Now i cannot see a method within the .NET class libaries of
> saying
> something like MoveToElement(LinePos,CharPos) or indeed a way of using
> XPath.
>
> The XmlTextReader does have a LineNumber and LinePosition property but
these
> are RO. I could use the XmlDocument's SelectNodes to find all the
occurences
> of the element named in the error but the node classes returned do not
> expose line or char postion info.
>
> Does anyone have any ideas?
>
> Thanks
>
> Pat Long
>
>
>
>
-
Re: Mapping validation error to DOM document
"Patrick Long" <Patrick_long@csi.com> wrote:
>When using the .NET ValidatingReader functionality you can get an error
list
>of
>invalid xml lines or a schema exception, according to the schema . Both
of
>these give you the element name and the line number and position e.g. Line
>56, Column 4, MyNameSpace:LastName.
>
>I want to be able to map this error data, in code, back to the xml being
>validated. Now i cannot see a method within the .NET class libaries of
>saying
>something like MoveToElement(LinePos,CharPos) or indeed a way of using
>XPath.
>
>The XmlTextReader does have a LineNumber and LinePosition property but these
>are RO. I could use the XmlDocument's SelectNodes to find all the occurences
>of the element named in the error but the node classes returned do not
>expose line or char postion info.
>
>Does anyone have any ideas?
>
>Thanks
>
>Pat Long
>
>
>
>
-
Re: Mapping validation error to DOM document
>
> For validation errors, you could load the document--without
validation--find
> the 5th person node with an XPath query, and then conceivably fix the
> problem using DOM methods to replace the node with a valid node. For badly
> formed XML errors, you'll need to write custom code to find and fix the
> error by loading the XML file as a text file and parsing the data
yourself,
> because the XML parser won't load badly formed XML.
<<
We need to return to the UI team an XPath for each peice of bad data. They
have a way of mapping data items
to UI Elements suing the XPath. There is no way we could fix the Xml
ourselves, the user needs to be notified.
Your suggestion seems to suggest we record what elements we are validationg
ALWAYS no matter whether the doc turns out to be valid or not.I would prefr
to do the normal validation and then only take special action when there is
an error.I will have a look at it today.
Thanks
Pat Long
"Russell Jones" <arj1@northstate.net> wrote in message
news:3bb92b18$1@news.devx.com...
> It's true that there's no direct method for accomplishing what you want;
> however, it shouldn't be too difficult to develop. Because you know the
> element name and line number when the error occurs, by simply keeping a
> count for each element, you should be able to find the element you want
> based on the count when the error occurs. For example, if you have a
> document like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <people xmlns:company xmlns:emp=http://www.mycompany.com/employees
> version"1.0">
> <emp erson>data here</emp erson>
> <emp erson>data here</emp erson>
> <emp erson>data here</emp erson>
> <person>data here</emp erson>
> <em erson>data here</em:Person>
> <emp erson>data here</emp erson>
> <emp erson>data here</emp erson>
> </people>
>
> As you read the document, you'll receive a validation error on the 5th
> person element. If you add each element name to a collection as the reader
> encounters it, or increment the count the collection would contain:
> people, 1
> person, 5
>
> For validation errors, you could load the document--without
validation--find
> the 5th person node with an XPath query, and then conceivably fix the
> problem using DOM methods to replace the node with a valid node. For badly
> formed XML errors, you'll need to write custom code to find and fix the
> error by loading the XML file as a text file and parsing the data
yourself,
> because the XML parser won't load badly formed XML.
>
> HTH
> Russell Jones
> Sr. Web Development Editor,
> DevX.com
>
>
> "Patrick Long" <Patrick_long@csi.com> wrote in message
> news:3bb8e780@news.devx.com...
> > When using the .NET ValidatingReader functionality you can get an error
> list
> > of
> > invalid xml lines or a schema exception, according to the schema . Both
of
> > these give you the element name and the line number and position e.g.
Line
> > 56, Column 4, MyNameSpace:LastName.
> >
> > I want to be able to map this error data, in code, back to the xml being
> > validated. Now i cannot see a method within the .NET class libaries of
> > saying
> > something like MoveToElement(LinePos,CharPos) or indeed a way of using
> > XPath.
> >
> > The XmlTextReader does have a LineNumber and LinePosition property but
> these
> > are RO. I could use the XmlDocument's SelectNodes to find all the
> occurences
> > of the element named in the error but the node classes returned do not
> > expose line or char postion info.
> >
> > Does anyone have any ideas?
> >
> > Thanks
> >
> > Pat Long
> >
> >
> >
> >
>
>
-
Re: Mapping validation error to DOM document
Patrick:
Sorry, perhaps I wasn't clear. What I'm suggesting is that when an error
occurs, you save the line number and position, and then attempt to read the
document *again* with the non-validating XMLTextReader, keeping track of the
node count so you can build your XPath query. Because you won't care about
efficiency for this query, you don't need to know the element hierarchy, you
can just write a query like SelectSingleNode("//somenode[38]"), assuming the
error occured on the 38th appearance of <somenode>.
Hope that helps.
"Patrick Long" <Patrick_long@csi.com> wrote in message
news:3bb95b96$1@news.devx.com...
> >
> > For validation errors, you could load the document--without
> validation--find
> > the 5th person node with an XPath query, and then conceivably fix the
> > problem using DOM methods to replace the node with a valid node. For
badly
> > formed XML errors, you'll need to write custom code to find and fix the
> > error by loading the XML file as a text file and parsing the data
> yourself,
> > because the XML parser won't load badly formed XML.
> <<
> We need to return to the UI team an XPath for each peice of bad data. They
> have a way of mapping data items
> to UI Elements suing the XPath. There is no way we could fix the Xml
> ourselves, the user needs to be notified.
>
> Your suggestion seems to suggest we record what elements we are
validationg
> ALWAYS no matter whether the doc turns out to be valid or not.I would
prefr
> to do the normal validation and then only take special action when there
is
> an error.I will have a look at it today.
>
> Thanks
>
> Pat Long
>
>
>
>
> "Russell Jones" <arj1@northstate.net> wrote in message
> news:3bb92b18$1@news.devx.com...
> > It's true that there's no direct method for accomplishing what you want;
> > however, it shouldn't be too difficult to develop. Because you know the
> > element name and line number when the error occurs, by simply keeping a
> > count for each element, you should be able to find the element you want
> > based on the count when the error occurs. For example, if you have a
> > document like this:
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <people xmlns:company xmlns:emp=http://www.mycompany.com/employees
> > version"1.0">
> > <emp erson>data here</emp erson>
> > <emp erson>data here</emp erson>
> > <emp erson>data here</emp erson>
> > <person>data here</emp erson>
> > <em erson>data here</em:Person>
> > <emp erson>data here</emp erson>
> > <emp erson>data here</emp erson>
> > </people>
> >
> > As you read the document, you'll receive a validation error on the 5th
> > person element. If you add each element name to a collection as the
reader
> > encounters it, or increment the count the collection would contain:
> > people, 1
> > person, 5
> >
> > For validation errors, you could load the document--without
> validation--find
> > the 5th person node with an XPath query, and then conceivably fix the
> > problem using DOM methods to replace the node with a valid node. For
badly
> > formed XML errors, you'll need to write custom code to find and fix the
> > error by loading the XML file as a text file and parsing the data
> yourself,
> > because the XML parser won't load badly formed XML.
> >
> > HTH
> > Russell Jones
> > Sr. Web Development Editor,
> > DevX.com
> >
> >
> > "Patrick Long" <Patrick_long@csi.com> wrote in message
> > news:3bb8e780@news.devx.com...
> > > When using the .NET ValidatingReader functionality you can get an
error
> > list
> > > of
> > > invalid xml lines or a schema exception, according to the schema .
Both
> of
> > > these give you the element name and the line number and position e.g.
> Line
> > > 56, Column 4, MyNameSpace:LastName.
> > >
> > > I want to be able to map this error data, in code, back to the xml
being
> > > validated. Now i cannot see a method within the .NET class libaries of
> > > saying
> > > something like MoveToElement(LinePos,CharPos) or indeed a way of
using
> > > XPath.
> > >
> > > The XmlTextReader does have a LineNumber and LinePosition property but
> > these
> > > are RO. I could use the XmlDocument's SelectNodes to find all the
> > occurences
> > > of the element named in the error but the node classes returned do not
> > > expose line or char postion info.
> > >
> > > Does anyone have any ideas?
> > >
> > > Thanks
> > >
> > > Pat Long
> > >
> > >
> > >
> > >
> >
> >
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks