Search code examples
asp.netxmlvb.netwebformsinfopath

VB.NET - Find a string in a XML String


I am working on a project to read InfoPath XML files into a .NET form. I'm trying to get the .xsn version from the href in order to determine what version of the InfoPath form I should display. Since there is only 1 .xsn string in the XML File, I can use that, but I'm having trouble parsing out the file name.

http://servername/foldername/forms/fileNameV100.xsn

Solution

  • Here is an example of how you would parse the filename. You can use these techniques to parse out the version. Additional notes are in the comments of the code.

    Private Function ParseXsnFileName(ByVal strTarget As String) As String
    
        Dim strResult As String = String.Empty
    
        'Get the location of where the .xsn extension starts.
        Dim intExtensionLocation As Integer = strTarget.IndexOf(".xsn")
    
        If intExtensionLocation >= 0 Then
    
            'Now we will initiate a loop that iterates back character by character until we find 
            'the forward slash of the URL that preceedes the filename.
            Dim bolStartFound As Boolean = False
            Dim intCursor As Integer = intExtensionLocation
    
            Do Until intCursor = 0 OrElse bolStartFound
    
                If strTarget.Substring(intCursor, 1) = "/" Then
    
                    'Setting this to true exist the loop.
                    bolStartFound = True
    
                End If
    
                intCursor -= 1
    
            Loop
    
            If bolStartFound Then
    
                'We found all of the pieces we need to parse out the filename.
    
                'Add 2 because of the "intCursor -= 1" and because we don't want the / in the filename.
                Dim intStartLocation As Integer = intCursor + 2
    
                'Add 4 to StartLocation because we want the extension.
                'Subtract intStartLocation from intExtensionLocation to get the length.
                strResult = strTarget.Substring(intStartLocation, (intExtensionLocation - (intStartLocation + 4)))
    
            End If
    
        End If
    
        Return strResult
    
    End Function
    

    Example Usage:

        Dim strParseThis As String = "http://servername/foldername/forms/fileNameV100.xsn"
    
        Dim strFileName As String = ParseXsnFileName(strParseThis)