How to read XML in Vb.net?

1

I created a program that generates a XML equal to:

<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 that I need to get the <Grupos pasta= and <Video> ... </Video> values from a given group and read all subsequent <Grupos pasta= and <Video> ... </Video> . I've been looking for some time on the Internet some way to do this and the ones I found read all the content. I found some other ways too, but that I could not implement. The best code I could do was this, but it only reads the first group last:

 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

So can anyone help me with how do I do this?

Edit:

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

NowwhenIselectthetestefolderitselectswhat'sinsideit.

WhatIwouldliketocreateisonly1xmlper"root node" of TreeView . For example it has 2 "root knots" called Movies in the Tree.

    
asked by anonymous 27.09.2018 / 20:34

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 tags Video might look something like this:

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

I think it will not be worth getting the tag Grupo information because the video file information is in Video , but if you just want to do it almost in the same way:

Dim textSegs As IEnumerable(Of String) = _  
    From seg In doc.SelectNodes("//Grupo") _  
    Select seg.Attributes("pasta").Value
    
01.10.2018 / 10:51