VB.NET - 在XML String中查找字符串

时间:2015-08-25 14:17:00

标签: asp.net xml vb.net webforms infopath

我正在开发一个将InfoPath XML文件读入.NET表单的项目。我试图从href获取.xsn版本,以确定我应该显示哪个版本的InfoPath表单。由于XML文件中只有1个.xsn字符串,我可以使用它,但是我在解析文件名时遇到了问题。

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

1 个答案:

答案 0 :(得分:0)

以下是解析文件名的示例。您可以使用这些技术来解析版本。附加注释在代码的注释中。

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

示例用法:

    Dim strParseThis As String = "http://servername/foldername/forms/fileNameV100.xsn"

    Dim strFileName As String = ParseXsnFileName(strParseThis)