Results 1 to 9 of 9

Thread: Parsing XML into List view

  1. #1

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Parsing XML into List view

    Hey,

    I have written a function which logs into an FTP server and recursively lists all of the directories within a given folder into an XML string (example below).

    I basically want to parse this into a listview item in the format as follows "Level1/level2/level3/level4" (standard file path). I have given an example of the xml below. How would I go about achieving this?

    I have set up an xmltextreader, and then using a do while xmltextreader.read() I can check the current level of the node and add it that way, but it tends to only work half the time - is this the best approach?

    Code:
    <dirTree>
    <dir name="Highest level" />
    <dir name="Highest Level 2">
        <dir name="level 2" />
        <dir name="level 2a" />
        <dir name="level 2b">
            <dir name="level 3" />
        </dir>
        <dir name="level 2c" />
    </dir>
    <dir name="Highest Level 3" />
    <dir name="Highest Level 4" />
    </dirTree>
    Code:
    Highest Level
    Highest Level 2
    Highest Level 2/level 2
    Highest Level 2/level 2a
    Highest Level 2/level 2b
    Highest Level 2/level 2b/level3
    Highest Level 2/level 2c
    Highest Level 3
    Highest Level 4
    Regards,

    Adam.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Parsing XML into List view

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim xml = _
    5.         <dirTree>
    6.             <dir name="Highest level"/>
    7.             <dir name="Highest Level 2">
    8.                 <dir name="level 2"/>
    9.                 <dir name="level 2a"/>
    10.                 <dir name="level 2b">
    11.                     <dir name="level 3"/>
    12.                 </dir>
    13.                 <dir name="level 2c"/>
    14.             </dir>
    15.             <dir name="Highest Level 3"/>
    16.             <dir name="Highest Level 4"/>
    17.         </dirTree>
    18.  
    19.         'read up to 3 levels from xml + display in listview
    20.  
    21.         Dim items = From node In xml...<dir> _
    22.                     Select New String() {node.@name, node...<dir>.@name, node...<dir>...<dir>.@name}
    23.  
    24.         For Each i In items
    25.             Dim subItems() As String = (From s In i Where s <> Nothing Select s).ToArray
    26.             ListView1.Items.Add(New ListViewItem(subItems))
    27.         Next
    28.  
    29.     End Sub
    30. End Class

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Parsing XML into List view

    Paul, I don't think you can assume there is only a maximum depth of 3 levels. Since it is a file structure from an FTP server I'm sure the nesting can be 'infinitely' deep, so you will probably need some kind of recursion. I'm not sure if LINQ supports recursion (I think it does in some complicated way) so maybe LINQ is not the best option in this case.

  4. #4

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Parsing XML into List view

    Hiya,

    Basically that's the same problem that I currently have, that I can only get it to read x levels into the FTP file, not recursively. Now, I'm aware there is a maximum number, something like 30,000 folders, but I really wouldn't like to go to that much trouble.

    I've never used LINQ before, but will take a look at it

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Parsing XML into List view

    I would just create some recursive function that
    1) Creates a string with the name of the current node,
    2) Then for each child 'dir' node, append the result of this same function (recursively) to that string
    3) Return the string.

    Something like that should work. You could still use the XDocument class to get the child nodes very easily, something like
    Code:
    For Each childDir In dir...<dir>
    where 'dir' is an XElement containing the parent 'dir' node.
    Then you could use the @ symbol to quickly get the name attribute:
    Code:
    str &= dir.@name
    But I wouldn't use LINQ in this case. It is very useful, but I don't think recursion is very easy to do, if possible at all. Likely it is possible, but it would probably be quite difficult to even read and understand it, and in such cases I think that using LINQ is not the right idea. It is supposed to make things easier to read by avoiding things like nested loops, so making a very hard to read LINQ query versus creating a simple recursive function is a little backwards...

  6. #6
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: Parsing XML into List view

    here is an example although it uses tree view:
    http://support.microsoft.com/kb/308063

    I used it before and works fine.

  7. #7

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Parsing XML into List view

    Hey,

    I've taken that code and slightly adapted it to the following:

    VB.NET Code:
    1. Private Sub PopulateTree(ByVal xml As String)
    2.         Try
    3.             ' SECTION 1. Create a DOM Document and load the XML data into it.
    4.             Dim dom As New XmlDocument
    5.             dom.LoadXml(xml)
    6.  
    7.             ' SECTION 2. Initialize the treeview control.
    8.             TreeView1.Nodes.Clear()
    9.             TreeView1.Nodes.Add(New TreeNode(dom.DocumentElement.Name))
    10.             Dim tNode As New TreeNode()
    11.             tNode = TreeView1.Nodes(0)
    12.  
    13.             ' SECTION 3. Populate the TreeView with the DOM nodes.
    14.             AddNode(dom.DocumentElement, tNode)
    15.             TreeView1.ExpandAll()
    16.  
    17.         Catch xmlEx As XmlException
    18.             MessageBox.Show(xmlEx.Message)
    19.         Catch ex As Exception
    20.             MessageBox.Show(ex.Message)
    21.         End Try
    22.  
    23.     End Sub
    24.  
    25.     Private Sub AddNode(ByRef inXmlNode As XmlNode, ByRef inTreeNode As TreeNode)
    26.         Dim xNode As XmlNode
    27.         Dim tNode As TreeNode
    28.         Dim nodeList As XmlNodeList
    29.         Dim i As Integer
    30.  
    31.         ' Loop through the XML nodes until the leaf is reached.
    32.         ' Add the nodes to the TreeView during the looping process.
    33.         If inXmlNode.HasChildNodes() Then
    34.             nodeList = inXmlNode.ChildNodes
    35.             For i = 0 To nodeList.Count - 1
    36.                 xNode = inXmlNode.ChildNodes(i)
    37.                 inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
    38.                 tNode = inTreeNode.Nodes(i)
    39.                 AddNode(xNode, tNode)
    40.             Next
    41.         Else
    42.             ' Here you need to pull the data from the XmlNode based on the
    43.             ' type of node, whether attribute values are required, and so forth.
    44.             inTreeNode.Text = (inXmlNode.OuterXml).Trim
    45.         End If
    46.     End Sub

    Just so that I can call it from a different procedure, and input the XML as a string rather than a file name. I also think that my problem lies with the "AddNode(dom.DocumentElement, tNode)" line. But all the tree nodes appear as "<dir foldername />" rather than "foldername"

    Is there anyway to change this?

    Regards

  8. #8
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: Parsing XML into List view

    I did not check it, but change this line:
    inTreeNode.Text = (inXmlNode.OuterXml).Trim

    to whatever the property. I guess the outerxml = <dir .../>, so, just denug it, and change it.

    it could be innertext, or something similar.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Parsing XML into List view

    nice recursion:

    vb Code:
    1. Private Sub AddNode(ByRef inXmlNode As XmlNode, ByRef inTreeNode As TreeNode)
    2.  
    3.     Dim xNode As XmlNode
    4.     Dim tNode As TreeNode
    5.     Dim nodeList As XmlNodeList
    6.     Dim i As Integer
    7.  
    8.     ' Loop through the XML nodes until the leaf is reached.
    9.     ' Add the nodes to the TreeView during the looping process.
    10.  
    11.     If inXmlNode.HasChildNodes() Then
    12.         nodeList = inXmlNode.ChildNodes
    13.  
    14.         For i = 0 To nodeList.Count - 1
    15.             xNode = inXmlNode.ChildNodes(i)
    16.             inTreeNode.Nodes.Add(New TreeNode(xNode.Attributes("name").Value))
    17.             tNode = inTreeNode.Nodes(i)
    18.             AddNode(xNode, tNode)
    19.         Next
    20.  
    21.     Else
    22.  
    23.         ' Here you need to pull the data from the XmlNode based on the
    24.         ' type of node, whether attribute values are required, and so forth.
    25.         inTreeNode.Text = inXmlNode.Attributes("name").Value
    26.  
    27.     End If
    28.  
    29. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width