Results 1 to 29 of 29

Thread: XML Validation - e.exception.lineposition/number

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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?
    Last edited by Radjesh Klauke; Dec 31st, 2013 at 03:23 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: XML Validation - e.exception.lineposition/number

    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.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: XML Validation - e.exception.lineposition/number

    @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!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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:



    Validation XSD:



    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: XML Validation - e.exception.lineposition/number

    vb.net Code:
    1. Try
    2.    Dim x As New XmlDocument
    3.    x.LoadXml(myxml) ' if myxml contains invalid xml at this point then ....
    4.  
    5. ' it goes straight to
    6.  
    7. Catch ex As XmlException
    8.    MessageBoxEx.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    9. End Try
    10.  
    11. ' without passing Go, without collecting 200 currency units, and leaving x as an empty document
    12.  
    13. ' it never validates according to schema only by the default xml standard
    14. ' and as there is no location information in this error message .... !
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: XML Validation - e.exception.lineposition/number

    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.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  13. #13

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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":

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  14. #14
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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..
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  15. #15

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.)


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  16. #16
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    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.
    Last edited by FunkyDexter; Jan 3rd, 2014 at 05:25 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  17. #17

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: XML Validation - e.exception.lineposition/number

    @FunkyDexter: You are 100% correct!
    You need anything from me to check it?


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  18. #18
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    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.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  19. #19

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.

    xml.zip


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  20. #20
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    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.
    Last edited by FunkyDexter; Jan 3rd, 2014 at 06:08 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  21. #21

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  22. #22
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    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.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  23. #23

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  24. #24
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    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.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  25. #25

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.
    Last edited by Radjesh Klauke; Jan 3rd, 2014 at 09:16 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  26. #26
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    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.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  27. #27

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  28. #28
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: XML Validation - e.exception.lineposition/number

    Quote Originally Posted by Radjesh Klauke View Post
    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!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  29. #29

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: XML Validation - e.exception.lineposition/number

    Sorry m8, was not my intention to be rude. If I offended you, I sincerely apologize.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width