-
XML Validation - e.exception.lineposition/number
Hi,
I'm validating an XML in my textbox against an xsd. This works fine, but it doesn't return the line-position(s) and number(s) where the error occurs.
I simply use:
Code:
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
MsgBox(e.Exception.ToString & vbnewline & e.exception.linenumber.tostring & vbnewline & e.exception.lineposition.tostring)
End sub
Position and Number return: 0
What can this be? If you need more code, let me know.
Thanks for the help in advance.
-
Re: XML Validation - e.exception.lineposition/number
Is it at all possible that the first error actually occurs at 0,0? If not then obviously we're going to need some idea of the actual validation method and the schema in use.
-
Re: XML Validation - e.exception.lineposition/number
Heya,
Thanks for the reply.
Here's the (partial) code:
Code:
Dim x As New XMLdocument
x.Loadxml(myrichtextintextbox)
Dim s As String = cstr(x.DocumentElement.Documenturi)
x.Schemas.Add(s, path_to_my_xsd)
Dim ehandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
x.Validate(ehandler)
When testing the validator he error doesn't occur at line 0 and position 0. It validates perfectly, but not giving me positions.
The schema I use is from http://www.microsoft.com/en-us/downl...s.aspx?id=1574 (way to big to copy and paste it here :P)
There is another slight issue I have. When I tested it I intentionally made 2 mistakes. Both mistakes should be caught and displayed in one go (if you get my grip). The code I use only catches one "mistake" at a time.
How to (re)code it, so I can catch them all in one go?
-
Re: XML Validation - e.exception.lineposition/number
Wait... it validates perfectly, but not giving you the positions? The positions of what? If it validates, it validates... if it doesn't THEN it should give you the positions... if it validates and there are no errors, then what should it be giving you?
If you tnen break the document so that it doesn't validate, it stops at the first validation error. The reason is that because the first error could cause many more later on.... or conversely, fixing the firt error could make more later, or fix all errors...
-tg
-
Re: XML Validation - e.exception.lineposition/number
Quote:
Dim x As New XMLdocument
x.Loadxml(myrichtextintextbox)
How are you overriding the automatic validation of xml that takes place on loading? If there's anything wrong in the text it should throw an exception immediately and never reach the 'deliberate' validation at all.
-
Re: XML Validation - e.exception.lineposition/number
I think I'm not very clear in explaining my issue. Let's try again. :P
The validation itself works. If there are no errors in the document nothing is thrown. If the document has any error it does throw an exception, BUT it doesn't return me any linenumer/position where the failure occurs. It only returns 0, where it should say: 6 (for example)
@dunfiddlin: I use a simple try-catch
-
Re: XML Validation - e.exception.lineposition/number
Quote:
@dunfiddlin: I use a simple try-catch
Umm. How? If the load fails and you go to a catch then the document is never loaded. Which would explain why you're getting an error at 0,0 because there's no content to verify!
-
Re: XML Validation - e.exception.lineposition/number
There's two possible types of errors... a load error, where the document just doesn't load... it's invalid XML ... example, you have an open tag at the top and never close it... that will prevent it from loading... so you need to make sure that it is VALID XML first...
After that, once you get it loaded, you THEN validate it against the schema... that is what's going to get you your position and all that. It's a two step process... step 1 is load the xml document... the second is to then validate it against the schema.
-tg
-
Re: XML Validation - e.exception.lineposition/number
Here's my code:
Code:
Try
Dim x As New XmlDocument
x.LoadXml(myxml)
validateschema = x.DocumentElement.NamespaceURI.ToString
Select Case validateschema
Case "http://schemas.microsoft.com/office/2006/01/customui"
xsdpath = librarypath & "schema\customUI.xsd"
Case "http://schemas.microsoft.com/office/2009/07/customui"
xsdpath = librarypath & "schema\customUI14.xsd"
Case Else
MessageBoxEx.Show("Invalid NameSpace detected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Select
x.Schemas.Add(validateschema, xsdpath)
Dim ehandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
x.Validate(ehandler)
Catch ex As XmlException
MessageBoxEx.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Invalid XML:
http://i.imgur.com/hQwXOC4.png
Validation XSD:
http://i.imgur.com/ZNgvHkX.png
As you can see I already check the xml using "catch". Perhaps it's something that I don't understand what you guys are trying to tell me.
-
Re: XML Validation - e.exception.lineposition/number
vb.net Code:
Try
Dim x As New XmlDocument
x.LoadXml(myxml) ' if myxml contains invalid xml at this point then ....
' it goes straight to
Catch ex As XmlException
MessageBoxEx.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
' without passing Go, without collecting 200 currency units, and leaving x as an empty document
' it never validates according to schema only by the default xml standard
' and as there is no location information in this error message .... !
-
Re: XML Validation - e.exception.lineposition/number
Am I going crazy here? Or I'm talking/typing in a strange language to you guys or I simply don't get it.
As you can see that the XML-exception itself does return the correct positions. It validates perfect. I only added the images to show you that it already catches if the document isn't correct. It has nothing to do with the XML-document itself. I'm talking about the validation against the XSD. The XML-validation is not the issue in this case.
If the XML isn't correct why should I validate it against the xsd? I don't see any point in doing that. If it's wrong, fix the XML. If the XML-document is correct then validate it against the XSD.
-
Re: XML Validation - e.exception.lineposition/number
Quote:
If the XML-document is correct then validate it against the XSD.
You are the one that claimed to have introduced errors into the text so it seems like a reasonable question to ask. So what 'errors' are you introducing that pass the XML validation but don't pass the schema validation? An example of the text you're passing would obviously be the most helpful thing here.
-
Re: XML Validation - e.exception.lineposition/number
Sorry, I thought it was clear with the images I uploaded. Okay, let me try again:
In this image I typed "supetip", which is wrong, cause it should be "supertip": http://www.vbforums.com/images/ieimages/2014/01/1.png
When I validate the text against the xsd it throws an exception, which is correct. When you look at the bottom of the image you see that it shows the exception. You also see "0 0". Those 2 numbers should be the linenumber and position. In this case it should give me "160 11", but as you can see, it doesn't.
-
Re: XML Validation - e.exception.lineposition/number
I've played around with this for an hour or so and don't seem to be able to introduce any error that is not detected by the standard xml validation before the secondary validation can be activated..
-
Re: XML Validation - e.exception.lineposition/number
Please stop talking about the standard XML validation! I beg you! The whole point of my question is about the xsd validation. Why are we still talking about the standard XML validation?
(on the other hand I don't understand why you don't get any error in the standard XML validation, don't know hat you did. It works for me.)
-
Re: XML Validation - e.exception.lineposition/number
I think people are indeed missing the point. Radjesh's XML is syntactically valid.
His first screen shot shows him deliberately introducing a syntax error (he deletes the closing bracket on </group>). I believe he included that to demonstrate that, in the case of a syntactical error, the line and position number ARE detectable. You can see this in the message box. It's not the point of his question, though.
The second screenshot shows him deliberately introducing a schema error (he's miss-spelt supertip). This is giving an error which can be seen at the bottom of the screen but the line number and position are being returned as zeros.
His qustion is nothing to do with the first screen shot which is actually just acting as a distraction at this point. His question is to do with the second screenshot. His xml syntax is valid but the document (correctly) fails to validate against the schema. His question is, why, when failing to validate against the schema, are the line number and position not reported.
Have I understood all that correctly, Radjesh?
I'm pretty sure that I normally get line and position number back in this scenario but I'd need to check to be 100% certain (I don't usually bother to report the position to the user). I think I've got a xml based app to hand so I'll try it out and let you know what I find.
edit> apologies, I don't have an xml based app to hand so can't test this without alot of setup.
-
Re: XML Validation - e.exception.lineposition/number
@FunkyDexter: You are 100% correct!
You need anything from me to check it?
-
Re: XML Validation - e.exception.lineposition/number
Actually yes. Attach the schema and the xml document with no errors. I should be able to knock up a single form app and observe what happens.
-
1 Attachment(s)
Re: XML Validation - e.exception.lineposition/number
I've attached a zip with a small XML-file and the schema. Hope you can figure why it doesn't return any position.
Attachment 109021
-
Re: XML Validation - e.exception.lineposition/number
Actually, I managed to find an xml app of my own so used that instead and it doesn't report the line number or position for me either so this is probably standard behaviour. Why it's standard behaviour is a mystery to me. I'd have thought it would be useful information. The only thing I can think of is that the message usually contains enough information to navigate to the error (it'll give you the node, the element that's incorrect etc.) so MS thought the line and position were redundant. You could maybe raise it as an issue on the apropriate MS forum and see what they say.
edit>Actually, the more I think about it the lack of line and position does make sense. In the case of an invalid child element you could report it but that's not the only type of schema error you can have. What about missing elements or elements appearing in the wrong order. What line would you report for these? I guess MS's thinking is that you should be using the information returned in the message to handle the error.
-
Re: XML Validation - e.exception.lineposition/number
Thanks. I started a new search on the internet and found a solution probably. http://social.msdn.microsoft.com/For...um=xmlandnetfx
Will let you know.
-
Re: XML Validation - e.exception.lineposition/number
That thread identifies that you just can't do it with the XML DOM classes as provided. One of the links that Martin Honen gives in that thread shows how to extend the DOM classes to add functionality to them but I'm afraid I have no idea how you'd actually apply it to the problem in hand. Using the linked example as a basis you could probably store an index number for each element and include that in the validation error message - so you'd be able to recognise that the problem was with the 2nd button element, for example, but that's not the same as a line number and position.
-
Re: XML Validation - e.exception.lineposition/number
Getting closer: http://connect.microsoft.com/VisualS...info-being-set
Do have issues implementing this. Will take a break and try later.
-
Re: XML Validation - e.exception.lineposition/number
Did you look at the workaround on that page? That looks like a pretty elegant way of handling it. Actually, because it's doing the load and the validation at the same time it really is elegant and, by the looks of it, it will allow you to retrieve multiple errors as well. I think I might use that technique myself in future.
-
Re: XML Validation - e.exception.lineposition/number
Yeah, I'm looking at the workaround, but don't get it how to implement it. Already converted it to vb.net btw, but to many errors in the code.
Code:
Private Shared Function GetSchemaErrors(stream As MemoryStream) As List(Of XmlSchemaException)
Dim settings As New XmlReaderSettings()
settings.Schemas = GetXmlSchemaSet()
settings.ValidationType = ValidationType.Schema
Dim errors As New List(Of XmlSchemaException)()
settings.ValidationEventHandler += Function(sender, args) errors.Add(args.Exception)
Dim reader As XmlReader = XmlReader.Create(stream, settings)
Dim doc = XDocument.Load(reader)
Return errors
End Function
Error 1 'GetXmlSchemaSet' is not declared. It may be inaccessible due to its protection level.
Error 2 'Public Event ValidationEventHandler(sender As Object, e As System.Xml.Schema.ValidationEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
Error 3 Option Strict On requires each lambda expression parameter to be declared with an 'As' clause if its type cannot be inferred.
Error 4 Option Strict On requires each lambda expression parameter to be declared with an 'As' clause if its type cannot be inferred.
Error 5 Option Strict On disallows late binding.
-
Re: XML Validation - e.exception.lineposition/number
You should be able to use it pretty much as posted (converted to VB firstobviously). The only change I'd make is that I'd make it a "Load" method that returns the xml document instead. Just before returning it would check errors.count and throw an apropriate exception if the count is greater than zero.
Mind you, I'm sitting here not having tried it so there may be all sorts of stings in the tail I've missed.
-
Re: XML Validation - e.exception.lineposition/number
Hahahaha. No problem. I added the errors in previous post. I'll check it later again. Really need a break, but I can't let it go. I know where close to a solution.
-
Re: XML Validation - e.exception.lineposition/number
Quote:
Originally Posted by
Radjesh Klauke
Please stop talking about the standard XML validation! I beg you! The whole point of my question is about the xsd validation. Why are we still talking about the standard XML validation?
(on the other hand I don't understand why you don't get any error in the standard XML validation, don't know hat you did. It works for me.)
I wasn't talking about it. I was saying I couldn't get beyond it so I couldn't check the result of the validation that you wanted. Sheesh!
-
Re: XML Validation - e.exception.lineposition/number
Sorry m8, was not my intention to be rude. If I offended you, I sincerely apologize.