Results 1 to 4 of 4

Thread: Get linenumber and position from standard XML validation

  1. #1

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

    Get linenumber and position from standard XML validation

    Now, you would say that this is simple, but in this example the validator returns 2 linenumbers and positions. And guess what!? I need the first linenumber and position in the message. I can only get the last position and linenumber using the ex.Position and ex.Linenumber. See Image below.



    My code:

    Code:
    msgbox(ex.Message.ToString & vbNewLine & "Linenumber: " & ex.LineNumber.ToString & vbNewLine & "Position: " & ex.LinePosition.ToString)
    I need to get/extract Line 3, Position 10 from the error.

    Anyone?


    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
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Get linenumber and position from standard XML validation

    Brute Force, see http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    Code:
    Dim xmlFrag As String = "<test>" + Chr(10) & _
                            "  <proper>" + Chr(10) & _
                            "  </proper>" + Chr(10) & _
                            "  <wrong>" + Chr(10) & _
                            "  <wrong>" + Chr(10) & _
                            "  <right>" + Chr(10) & _
                            "  </right>" + Chr(10) & _
                            "</test>"
    
    ' Create the XmlNamespaceManager. 
    Dim nsmgr As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(New Xml.NameTable())
    
    ' Create the XmlParserContext. 
    Dim context As Xml.XmlParserContext = New Xml.XmlParserContext(Nothing, nsmgr, Nothing, Xml.XmlSpace.None)
    
    ' Create the reader. 
    Dim reader As Xml.XmlValidatingReader = New Xml.XmlValidatingReader(xmlFrag, Xml.XmlNodeType.Element, context)
    
    Dim lineInfo As Xml.IXmlLineInfo = CType(reader, Xml.IXmlLineInfo)
    Dim ErrorMessage As String = ""
    If (lineInfo.HasLineInfo()) Then
    
    
        ' Parse the XML and display each node.
        Try
            While (reader.Read())
                Select Case reader.NodeType
                    Case Xml.XmlNodeType.Element
                        Console.Write("{0} {1},{2}  ", reader.Depth, lineInfo.LineNumber, lineInfo.LinePosition)
                        Console.WriteLine("<{0}>", reader.Name)
                    Case Xml.XmlNodeType.Text
                        Console.Write("{0} {1},{2}  ", reader.Depth, lineInfo.LineNumber, lineInfo.LinePosition)
                        Console.WriteLine("  {0}", reader.Value)
                    Case Xml.XmlNodeType.EndElement
                        Console.Write("{0} {1},{2}  ", reader.Depth, lineInfo.LineNumber, lineInfo.LinePosition)
                        Console.WriteLine("</{0}>", reader.Name)
                End Select
            End While
        Catch ex As Exception
            ErrorMessage = ex.Message
        End Try
    End If
    
    ' Close the reader.
    reader.Close()
    
    If Not String.IsNullOrWhiteSpace(ErrorMessage) Then
        Console.WriteLine()
        Console.WriteLine(ErrorMessage)
    End If
    Results
    0 1,2 <test>
    1 2,4 <proper>
    1 3,5 </proper>
    1 4,4 <wrong>
    2 5,4 <wrong>
    3 6,4 <right>
    3 7,5 </right>

    The 'wrong' start tag on line 5 position 4 does not match the end tag of 'test'. Line 8, position 3.

  3. #3

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

    Re: Get linenumber and position from standard XML validation

    heya kevin. Thanks for the info. The issue here is that i only want to return (when we take your example) "line 5, position 4". The rest is not really interesting 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

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Get linenumber and position from standard XML validation

    Quote Originally Posted by Radjesh Klauke View Post
    heya kevin. Thanks for the info. The issue here is that i only want to return (when we take your example) "line 5, position 4". The rest is not really interesting for me.
    Well I am sure this could be done in Regex but here is a brute force method. Mocked represents our exception message. Yes this can be optimized in string functionality, I did this fast and it works.
    Code:
    Dim Mocked As String = "The 'wrong' start tag on line 5 position 4 does not match the end tag of 'test'. Line 8, position 3."
    
    If Mocked.ToLower.Contains("line") AndAlso Mocked.ToLower.Contains("position") AndAlso Mocked.ToLower.Contains("does") Then
        Dim Index As Integer = Mocked.ToLower.IndexOf("line") + 4
        Dim Temp As String = Mocked.Substring(Index).TrimStart
        Index = Temp.ToLower.IndexOf("position")
        Console.WriteLine("Line index is " & Temp.Substring(0, Index))
        Temp = Temp.Substring(Index)
        Temp = Temp.Replace("position", "").TrimStart
        Index = Temp.ToLower.IndexOf("does")
        Console.WriteLine("Postion is " & Temp.Substring(0, Index))
    End If

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