2015-08-25 2 views
-1

Je travaille sur un projet de lecture de fichiers XML InfoPath dans un formulaire .NET. J'essaie d'obtenir la version .xsn de l'href afin de déterminer quelle version du formulaire InfoPath je devrais afficher. Comme il n'y a qu'une seule chaîne .xsn dans le fichier XML, je peux l'utiliser, mais j'ai de la difficulté à analyser le nom du fichier.VB.NET - Rechercher une chaîne dans une chaîne XML

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

Êtes-vous juste essayer d'analyser le nom de fichier d'un 'CHAINE' ou êtes-vous en utilisant quelque chose comme' XPath'? – jradich1234

+0

Essayez simplement d'analyser la chaîne dans la partie "nom de fichier" du chemin. –

Répondre

0

Voici un exemple d'analyse du nom de fichier. Vous pouvez utiliser ces techniques pour analyser la version. Des notes supplémentaires sont dans les commentaires du 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 

Exemple d'utilisation:

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

    Dim strFileName As String = ParseXsnFileName(strParseThis) 
+0

Merci! C'est exactement ce dont j'avais besoin –

+0

De rien. Veuillez marquer ceci comme la réponse acceptée afin que cela ne reste pas comme une question en suspens sans une réponse acceptée. – N0Alias