Aide sur manipulation XML

Résolu
Sentynel Messages postés 85 Date d'inscription jeudi 7 janvier 2010 Statut Membre Dernière intervention 13 juillet 2013 - 29 déc. 2011 à 21:38
Sentynel Messages postés 85 Date d'inscription jeudi 7 janvier 2010 Statut Membre Dernière intervention 13 juillet 2013 - 30 déc. 2011 à 11:52
Bonjour à tous.
J'ai un petit problème concernant une manipulation XML.

Voici le fichier XML :

<?xml version="1.0" encoding="utf-8" ?>

<Games>
  <Game title="GrandTheftAuto3" name="Grand Theft Auto 3 : 10th Anniversary Edition">
    <location_path environment_variable="%SDCARD%" path="\Android\data\com.rockstar.gta3\files\GTA3"/>
    
    <file0_path filename="gta3.set"/>
    <file1_path filename="GTA3sf1.b"/>
    <file2_path filename="GTA3sf2.b"/>
    <file3_path filename="GTA3sf3.b"/>
    <file4_path filename="GTA3sf4.b"/>
    <file5_path filename="GTA3sf5.b"/>
    <file6_path filename="GTA3sf6.b"/>
    <file7_path filename="GTA3sf7.b"/>
    <file8_path filename="GTA3sf8.b"/>
    <file9_path filename="GTA3sf9.b"/>
    <storage_path environment_variable="%STORAGE%" path="\Grand Theft Auto 3"/>
  </Game>
</Games>


Le logiciel ira detecter automatiquement la variable %SDCARD%,
la variable FolderOnly sert a determiner si on doit enregistrer le dossier ou les fichiers,
la variable %STORAGE% sera definie dans une textbox.

Dans ce cas, disons que SDCARD G:\, que STORAGE C:\Test\ et qu'on veuille enregistrer les fichiers déterminé dans le dossier "Android\data\com.rockstar.gta3\files\GTA3"

Dans ma form, il y a une listbox,
un bouton pour vérifier si les chemins existent,
un bouton pour enregistrer les chemins sélectionné.

Voici le code pour verifié les chemins :

            Dim doc As XDocument = XDocument.Load(DatabaseLocation)
            Dim games As IEnumerable(Of XElement) = From g In doc.Descendants("Game")

            For Each game As XElement In games
                Dim first As XElement = DirectCast(game.FirstNode, XElement)
                Dim last As XElement = DirectCast(game.LastNode, XElement)

                If System.IO.Directory.Exists(Update_Path(first.FirstAttribute.Value) & first.LastAttribute.Value) Then
                    ListBoxGames.Items.Add(game.Attribute(game.FirstAttribute.NextAttribute.Name).Value)
                End If
            Next


Le problème c'est que pour enregistrer, je n'arrive pas a faire appelle a la variable "FolderOnly" et les fichiers qui vont avec...

Quelqu'un aurait-il une solution ? Merci

6 réponses

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
30 déc. 2011 à 08:45
faire par exemple :

        Dim doc As XDocument = XDocument.Load("game.xml")
        Dim environment_variable As String
        Dim path As String
        Dim IsFolderOnly As Boolean

        For Each Game As XElement In doc.Descendants("Game")
            IsFolderOnly Game.Element("IsFolderOnly").Attribute("FolderOnly") "YES"
            With Game.Element("location_path")
                environment_variable = .Attribute("environment_variable").Value
                path = .Attribute("path").Value
            End With
            If Directory.Exists(System.IO.Path.Combine(Update_Path(environment_variable), path)) Then
                ListBoxGames.Items.Add(Game.Attribute("name").Value)
            End If
            If IsFolderOnly Then
                '# [...]
            End If
            For Each File As XElement In (From Item In Game.Elements Where Item.Name.ToString() Like "file*_path")
                ListBox2.Items.Add(File.Attribute("filename").Value)
            Next
        Next



Renfield - Admin CodeS-SourceS - MVP Visual Basic & Spécialiste des RegExp
3
Sentynel Messages postés 85 Date d'inscription jeudi 7 janvier 2010 Statut Membre Dernière intervention 13 juillet 2013
29 déc. 2011 à 22:29
Voici peu-être un petit debut...
bien que le code soit long et que certains les fichiers ne sont pas enregistrer si la valeur du FolderOnly est "YES"

        Dim doc As XDocument = XDocument.Load(DatabaseLocation)
        Dim games As IEnumerable(Of XElement) = From g In doc.Descendants("Game")

        For Each game As XElement In games
            Dim first As XElement = DirectCast(game.FirstNode, XElement)
            Dim IsFolderOnly As XElement = DirectCast(game.FirstNode.NextNode, XElement)
            Dim File0 As XElement = DirectCast(game.FirstNode.NextNode.NextNode, XElement)
            Dim File1 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode, XElement)
            Dim File2 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode.NextNode, XElement)
            Dim File3 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode.NextNode.NextNode, XElement)
            Dim File4 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode, XElement)
            Dim File5 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode, XElement)
            Dim File6 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode, XElement)
            Dim File7 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode, XElement)
            Dim File8 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode, XElement)
            Dim File9 As XElement = DirectCast(game.FirstNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode.NextNode, XElement)
            Dim last As XElement = DirectCast(game.LastNode, XElement)

            If ListBoxGames.Items.Contains(game.Attribute(game.FirstAttribute.NextAttribute.Name).Value) Then
                If System.IO.Directory.Exists(Update_Path(first.FirstAttribute.Value) & first.LastAttribute.Value) = True Then
                    If IsFolderOnly.FirstAttribute.Value = "NO" Then
                        If My.Computer.FileSystem.FileExists(File0.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File0.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File1.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File1.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File2.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File2.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File3.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File3.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File4.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File4.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File5.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File5.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File6.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File6.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File7.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File7.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File8.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File8.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                        If My.Computer.FileSystem.FileExists(File9.FirstAttribute.Value) Then
                            My.Computer.FileSystem.CopyFile(File9.FirstAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value)
                            Me.Refresh()
                        End If
                    End If
                    If IsFolderOnly.FirstAttribute.Value = "YES" Then
                        My.Computer.FileSystem.CopyDirectory(Update_Path(first.FirstAttribute.Value) & first.LastAttribute.Value, Update_Path(last.FirstAttribute.Value) & last.LastAttribute.Value, True)
                        Me.Refresh()
                    End If
                End If
            End If
        Next
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
30 déc. 2011 à 08:24
FirstAttribute ?

pas sain du tout, ce genre de code...

Ca ne sert a rien de jouer avec de l'XML si le jour ou on ajoute un attribut, on doit tout recoder...

se baser sur le NOM de l'attribut !

Renfield - Admin CodeS-SourceS - MVP Visual Basic & Spécialiste des RegExp
0
Sentynel Messages postés 85 Date d'inscription jeudi 7 janvier 2010 Statut Membre Dernière intervention 13 juillet 2013
30 déc. 2011 à 09:41
Oh !! Merci beaucoup!
en effet a chaque fois que j'ajoutais un attribut j'etais obliger de tout refaire
Pour le IsFolderOnly "YES" j'ai trouver une solution.

A bientôt !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
30 déc. 2011 à 10:16
Pour le IsFolderOnly "YES" j'ai trouver une solution.


? une solution à quoi ?

Renfield - Admin CodeS-SourceS - MVP Visual Basic & Spécialiste des RegExp
0
Sentynel Messages postés 85 Date d'inscription jeudi 7 janvier 2010 Statut Membre Dernière intervention 13 juillet 2013
30 déc. 2011 à 11:52

? une solution à quoi ?


Dans mon deuxième poste j'ai expliquer que j'avais un problème lors de l'enregistrement des fichiers si la valeur de IsFolderOnly était YES. J'avais juste oublier de préciser le chemin complet...
0
Rejoignez-nous