How to read XML in Vb.net?

Asked

Viewed 290 times

2

I created a program that generates a XML as is:

<Pastas3>
  <Grupos pasta="C:\...\Arquivos\Videos">
    <Video>C:\...\Arquivo 1</Video>
    <Video>C:\...\Arquivo 2</Video>
    <Video>C:\...\Arquivo 3</Video>
    <Video>C:\...\Arquivo 4</Video>
   <Grupos pasta="C:\...\Arquivos\Videos\Videos">
      <Video>C:\...\Arquivo 1</Video>
      <Video>C:\...\Arquivo 2</Video>
      <Video>C:\...\Arquivo 3</Video>
      <Video>C:\...\Arquivo 4</Video>
      <Grupos pasta="C:\...\Arquivos\Videos\Videos\Video1">
      <Video>C:\...\Arquivo 1</Video>
      <Video>C:\...\Arquivo 2</Video>
      <Video>C:\...\Arquivo 3</Video>
      <Video>C:\...\Arquivo 4</Video>
      </Grupos>
      <Grupos pasta="C:\...\Arquivos\Videos\Video\Video 2">
      <Video>C:\...\Arquivo 1</Video>
      <Video>C:\...\Arquivo 2</Video>
      <Video>C:\...\Arquivo 3</Video>
      <Video>C:\...\Arquivo 4</Video>
       <Grupos pasta="C:\...\Arquivos\Videos\Video\Video 2\Algo">
         <Video>C:\...\Arquivo 1</Video>
         <Video>C:\...\Arquivo 2</Video>
         <Video>C:\...\Arquivo 3</Video>
         <Video>C:\...\Arquivo 4</Video>
        </Grupos>
      </Grupos>
   </Grupos>
   <Grupos pasta="C:\...\Arquivos\Videos">
         <Video>C:\...\Arquivo 1</Video>
         <Video>C:\...\Arquivo 2</Video>
         <Video>C:\...\Arquivo 3</Video>
         <Video>C:\...\Arquivo 4</Video>
   </Grupos>
  </Grupos>
</Pastas3>

The problem is I need to get the figures <Grupos pasta= and <Video> ... </Video> from a particular group and read all the <Grupos pasta= and <Video> ... </Video> subsequent. I’ve been looking for some way to do this on the Internet and the ones I found read all the content. I found some other ways too, but I couldn’t implement them. The best code I could do was this, but it only reads the first group past:

 Private Sub ColocarList()
    Dim xd As XmlDocument = New XmlDocument
    xd.Load("C:\Users\lourd\source\repos\Teste_Na a\Teste_Na a\xml.xml")

    Dim n As XmlNode = xd.DocumentElement()
    DaCerto(n)
End Sub


Private Sub DaCerto(z As XmlNode)


    Dim gp As New ListViewGroup

    For Each node As XmlNode In z
        If node.Attributes.GetNamedItem("pasta").InnerText <> Nothing Then
            gp = New ListViewGroup(node.Attributes.GetNamedItem("pasta").InnerText) With {
                .Name = node.Attributes.GetNamedItem("pasta").InnerText
                    }
            ListView1.Groups.Add(gp)

            For i = 0 To node.ChildNodes.Count - 1
                If node.ChildNodes(i).Name = "Video" Then

                    ColocarPastasDestroDoListView(node.ChildNodes(i).InnerText, gp)

                ElseIf node.ChildNodes(i).Name = "Grupo" Then
                    DaCerto(node.ChildNodes(i))
                End If
            Next
        End If
    Next


End Sub

Then someone can help me with how I do it?

Edit:

When I select the folder Filmes it creates groups with the name of the folders and their content goes into the group in the ListView.

inserir a descrição da imagem aqui

Now when I select the folder teste he selects what’s inside her.

inserir a descrição da imagem aqui

What I would like is to create only 1 xml by "root node" of TreeView. For example has 2 "root node" called Movies in Tree.

  • I don’t think your XML is well structured... Grupos inside Grupos? Shouldn’t have a SubGrupo within a Grupo?

  • I never used XML and kind of put the cart in front of the donkeys. The xml I need has basically 2 information the Atributo groups and the value in Video. So I’ll need to create a name for each group?

1 answer

0

Your XML should be something like this:

<Grupos>
    <Grupo pasta="C:\...\Arquivos\Videos">
        <Videos>
            <Video>C:\...\Arquivo 1</Video>
            <Video>C:\...\Arquivo 2</Video>
            <Video>C:\...\Arquivo 3</Video>
            <Video>C:\...\Arquivo 4</Video>
        </Videos>
    </Grupo>
    <Grupo pasta="C:\...\Arquivos\Videos\Videos">
        <Videos>
            <Video>C:\...\Arquivo 1</Video>
            <Video>C:\...\Arquivo 2</Video>
            <Video>C:\...\Arquivo 3</Video>
            <Video>C:\...\Arquivo 4</Video>
        </Videos>
    </Grupo>
    <Grupo pasta="C:\...\Arquivos\Videos\Videos\Video1">
        <Videos>
            <Video>C:\...\Arquivo 1</Video>
            <Video>C:\...\Arquivo 2</Video>
            <Video>C:\...\Arquivo 3</Video>
            <Video>C:\...\Arquivo 4</Video>
        </Videos>
    </Grupo>
    <!-- ... -->
</Grupos>

Then the code to get all the tags Video might be something like this:

Dim textSegs As IEnumerable(Of String) = _  
    From seg In root...<Video> _  
    Select seg.Value

I don’t think it’s worth getting the information from tag Grupo because the video file information is in Video, but if you want just do almost the same way:

Dim textSegs As IEnumerable(Of String) = _  
    From seg In doc.SelectNodes("//Grupo") _  
    Select seg.Attributes("pasta").Value
  • Thanks, but I had managed to make a code "functional" similar to this, but I had the following problem: I made a program that lists recursively the folders and the files in them, only if I choose the folders C:\...\imagem and then C:\...\Arquivos and then go back to the folder C:\...\imagem it will redo the "calculations" being that it already made them, so I wanted to use xml as a type of database so that instead of redoing everything it simply saves in an xml and in the next access it only loads the xml (I accept suggestions of other ways of how to do this).

  • How I do recursively it take the files for example from folder C:\...\Arquivos it will grab some files from that folder, move to the subfolders and then go back to C:\...\Arquivos. So I tried to do it that way, put it that way I’d just need to take a group and their subgroups. And if I did so I would have to create several xml files, one for each group. One xml to get everything you have from the folder C:\...\Arquivos, one to pick up from C:\...\Arquivos\algo ...

  • Why not only save the location of the videos to XML? Because there are groups?

  • Yes, I create groups with the folder name in listview and place the files inside.

  • Sorry @Lucaspedro, but I don’t understand why the structure and the code I put in the answer aren’t enough :/

  • I’m sorry, it’s a little confusing, but I’ll try to explain it another way. I have a program similar to File Explorer windows, with a TreeView and a ListView and when I pass for example the way C:\...\imagem, the TreeView will show all folders and subfolders of that path, and when it was selected for example the path C:\...\imagem would be created in the ListView groups with the name of the folders and subfolders and within the groups would be the information of the files that are contained within the folder. Only depending on the amount of files this could take.

  • That’s why I was trying to use xml to "speed up" data loading. This way you went is correct, but I would have to make one xml file per folder, one to get everything you have in the folder and subfolders C:\...\imagem another to grab folders and subfolders from C:\...\imagem\algo, and so on. So if I had 50 folders I would have to create 50 xml. So I wanted to know if you have how to put the "group" C:\...\imagem\algo within the "group" C:\...\imagem and so on.

  • So that when I selected the "group" C:\...\imagem it only takes the groups and subgroups that are within the group C:\...\imagem and in that same xml when I selected from the "group" C:\...\imagem\algo it only takes the groups and subgroups that are within the group C:\...\imagem\algo.

  • Supposed to just need the tag <Video>, and then to build the ListView treated the paths to create the groups. You can place an example image in your question of how you want to have the ListView?

  • I edited, I put the images.

  • The goal of XML is to give the information to build the ListView right-hand side or to TreeView on the left side?

  • Build the ListView.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.