Results 1 to 10 of 10

Thread: [RESOLVED]Iterating XML Data to a Listview

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    [RESOLVED]Iterating XML Data to a Listview

    Hi, all. I'm new here, but I always exhaust myself in research before I go asking for help with my code. What I'm actually doing is porting a program I wrote in AutoIT proprietary code over to vb by hand. I've really come far and have figured out out how to read/write data to an XML file, which was the biggest challenge. I'm having a little bit of a rough spot with iterating nodelist data and then adding it to a listview in order. Here's what I have within a sub that begins to pull data from the file.

    Code:
            Dim xmldoc As New System.Xml.XmlDocument()
                Dim nodelisttaken As XmlNodeList
                Dim nodelisttime As XmlNodeList
                Dim nodelistaccount As XmlNodeList
                Dim nodelistproduct As XmlNodeList
            Dim node As XmlNode
            Dim datestrip As String
            datestrip = "date" & Replace(MonthCalendar1.SelectionStart, Chr(47), Chr(46))
    
            xmldoc.Load(Application.StartupPath + "\data.xml")
    
    
                nodelisttaken = xmldoc.SelectNodes("/root/calllog/" & datestrip & "/*")
                nodelisttime = xmldoc.SelectNodes("/root/calllog/" & datestrip & "//time")
                nodelistaccount = xmldoc.SelectNodes("/root/calllog/" & datestrip & "//account")
                nodelistproduct = xmldoc.SelectNodes("/root/calllog/" & datestrip & "//product")
    
    
            For Each node In nodelisttime
    
                recorddump.Items.Add(node.InnerText)
    
    next
    So, as you can see, there are some predefined nodelists for the data that I want to grab. "recorddump" is my listview control name, so this very nicely takes the data I ask for and adds it to the first column. But the problems begin here. I read about adding data to columns and what I find is to use something like

    Code:
    dim object as listviewitem
    object = recorddump.items.add(node.innertext)
    with object
    .subitems.add(node.innertext)
    .subitems.add(node.innertext)
    .subitems.add(node.innertext)
    .imageindex = 0
    endwith
    This adds data a row at a time, which is what I want. But look at the data I'm feeding it, node.innertext. I have to iterate the specific nodelist before I can use node.innertext. Putting .subitems.add in the iteration just results in a huge mess. I thought about using a function to return the data and then drop the function name in there, but whenever I iterate within the function it only ever returns the first item (it iterates only once even though there are more than one interations). Hopefully someone can point me in the right direction, I don't need to be spoonfed the answer.
    Last edited by JetsterDajet; Apr 26th, 2009 at 04:32 PM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Iterating XML Data to a Listview

    I've been banging my head against this for days. I've tried some different things with indecies, but it's always a mess. I keep trying to make a function that returns the innertext of the nodelist as an array... but I just can't get it.

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Iterating XML Data to a Listview

    Please post the output from node.outerxml
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Iterating XML Data to a Listview

    node.outerxml returns

    <date4.24.2009>date4.24.2009<call1>calldata<time>time11.55.06 </time><account>No Account Data Found </account><product>High Speed Internet </product><issue>Unable To Login to .com/net </issue><resolution>Issue Unresolved - Not At Location </resolution></call1><call2>calldata<time>time11.55.09 </time><account>No Account Data Found </account><product>High Speed Internet </product><issue>Unable To Browse </issue><resolution>Issue Resvoled - Modem Was In Standby </resolution></call2><call3>calldata<time>time11.55.12 </time><account>No Account Data Found </account><product>High Speed Internet </product><issue>Ticket Status </issue><resolution>Issue Resolved - Ticket Status Provided </resolution></call3><call4>calldata<time>time11.55.29 </time><account>No Account Data Found </account><product>High Speed Internet </product><issue>Ticket Status </issue><resolution>Issue Resolved - Ticket Status Provided </resolution></call4><call5>calldata<time>time11.55.32 </time><account>No Account Data Found </account><product>Digital Voice </product><issue>Disco - Non Pay Status </issue><resolution>Issue Unresolved - Cust Will Pay Later </resolution></call5><call6>calldata<time>time11.55.42 </time><account>No Account Data Found </account><product>High Speed Internet </product><issue>Unable To Login to .com/net </issue><resolution>Issue Resolved - Cleared Cookies/Cache </resolution></call6></date4.24.2009>

    This is organized as six calls and thier associated data. Do you think there's a better way of storing my information that may make it easier to iterate?

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Iterating XML Data to a Listview

    I would store the date stamp in an attribute, remove the calldata text, remove the word time from the time stamp, so and create an attribute for call id, your data would look like this
    Code:
    <date stamp="4.24.2009">
      <call id="1">
        <time>11.55.06 </time>
        <account>No Account Data Found </account>
        <product>High Speed Internet </product>
        <issue>Unable To Login to .com/net </issue>
        <resolution>Issue Unresolved - Not At Location </resolution>
      </call>
      <call id="2">
        <time>11.55.09 </time>
        <account>No Account Data Found </account>
        <product>High Speed Internet </product>
        <issue>Unable To Browse </issue>
        <resolution>Issue Resvoled - Modem Was In Standby </resolution>
      </call>
      <call id="3">
        <time>11.55.12 </time>
        <account>No Account Data Found </account>
        <product>High Speed Internet </product>
        <issue>Ticket Status </issue>
        <resolution>Issue Resolved - Ticket Status Provided </resolution>
      </call>
      <call id="4">
        <time>11.55.29 </time>
        <account>No Account Data Found </account>
        <product>High Speed Internet </product>
        <issue>Ticket Status </issue>
        <resolution>Issue Resolved - Ticket Status Provided </resolution>
      </call>
      <call id="5">
        <time>11.55.32 </time>
        <account>No Account Data Found </account>
        <product>Digital Voice </product>
        <issue>Disco - Non Pay Status </issue>
        <resolution>Issue Unresolved - Cust Will Pay Later </resolution>
      </call>
      <call id="6">
        <time>11.55.42 </time>
        <account>No Account Data Found </account>
        <product>High Speed Internet </product>
        <issue>Unable To Login to .com/net </issue>
        <resolution>Issue Resolved - Cleared Cookies/Cache </resolution>
      </call>
    </date>
    Last edited by wild_bill; Apr 24th, 2009 at 03:23 PM.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Iterating XML Data to a Listview

    Agreed, that seems to be a better way to organize the data, thank you, I'll make the changes in the compiler. But it still leaves me with the issue of having to iterate the data before I can add it to a listview, I'm still not sure how to deal with that.

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Iterating XML Data to a Listview

    Code:
            Dim xmlData As String = _
     "<date stamp=""4.24.2009"">" & Environment.NewLine & _
    "  <call id=""1"">" & Environment.NewLine & _
    "    <time>11.55.06 </time>" & Environment.NewLine & _
    "    <account>No Account Data Found </account>" & Environment.NewLine & _
    "    <product>High Speed Internet </product>" & Environment.NewLine & _
    "    <issue>Unable To Login to .com/net </issue>" & Environment.NewLine & _
    "    <resolution>Issue Unresolved - Not At Location </resolution>" & Environment.NewLine & _
    "  </call>" & Environment.NewLine & _
    "  <call id=""2"">" & Environment.NewLine & _
    "    <time>11.55.09 </time>" & Environment.NewLine & _
    "    <account>No Account Data Found </account>" & Environment.NewLine & _
    "    <product>High Speed Internet </product>" & Environment.NewLine & _
    "    <issue>Unable To Browse </issue>" & Environment.NewLine & _
    "    <resolution>Issue Resvoled - Modem Was In Standby </resolution>" & Environment.NewLine & _
    "  </call>" & Environment.NewLine & _
    "  <call id=""3"">" & Environment.NewLine & _
    "    <time>11.55.12 </time>" & Environment.NewLine & _
    "    <account>No Account Data Found </account>" & Environment.NewLine & _
    "    <product>High Speed Internet </product>" & Environment.NewLine & _
    "    <issue>Ticket Status </issue>" & Environment.NewLine & _
    "    <resolution>Issue Resolved - Ticket Status Provided </resolution>" & Environment.NewLine & _
    "  </call>" & Environment.NewLine & _
    "  <call id=""4"">" & Environment.NewLine & _
    "    <time>11.55.29 </time>" & Environment.NewLine & _
    "    <account>No Account Data Found </account>" & Environment.NewLine & _
    "    <product>High Speed Internet </product>" & Environment.NewLine & _
    "    <issue>Ticket Status </issue>" & Environment.NewLine & _
    "    <resolution>Issue Resolved - Ticket Status Provided </resolution>" & Environment.NewLine & _
    "  </call>" & Environment.NewLine & _
    "  <call id=""5"">" & Environment.NewLine & _
    "    <time>11.55.32 </time>" & Environment.NewLine & _
    "    <account>No Account Data Found </account>" & Environment.NewLine & _
    "    <product>Digital Voice </product>" & Environment.NewLine & _
    "    <issue>Disco - Non Pay Status </issue>" & Environment.NewLine & _
    "    <resolution>Issue Unresolved - Cust Will Pay Later </resolution>" & Environment.NewLine & _
    "  </call>" & Environment.NewLine & _
    "  <call id=""6"">" & Environment.NewLine & _
    "    <time>11.55.42 </time>" & Environment.NewLine & _
    "    <account>No Account Data Found </account>" & Environment.NewLine & _
    "    <product>High Speed Internet </product>" & Environment.NewLine & _
    "    <issue>Unable To Login to .com/net </issue>" & Environment.NewLine & _
    "    <resolution>Issue Resolved - Cleared Cookies/Cache </resolution>" & Environment.NewLine & _
    "  </call>" & Environment.NewLine & _
    "</date>"
    
            Dim doc As New Xml.XmlDocument
            doc.LoadXml(xmlData)
            ListView1.View = Windows.Forms.View.Details
            ListView1.FullRowSelect = True
            ListView1.GridLines = True
            ListView1.Columns.Add("Date Column", -2, HorizontalAlignment.Left)
            ListView1.Columns.Add("Time Column", -2, HorizontalAlignment.Left)
            ListView1.Columns.Add("Account Column", -2, HorizontalAlignment.Left)
            ListView1.Columns.Add("Product Column", -2, HorizontalAlignment.Left)
            ListView1.Columns.Add("Issue Column", -2, HorizontalAlignment.Left)
            ListView1.Columns.Add("Resolution Column", -2, HorizontalAlignment.Left)
            For Each dateNode As Xml.XmlNode In doc.ChildNodes
                AddDateNodeToListBox(dateNode, ListView1)
            Next
            ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
    Code:
        Private Sub AddDateNodeToListBox(ByVal dateNode As Xml.XmlNode, ByVal listView As ListView)
    
            Dim dateStamp = dateNode.Attributes("stamp").Value
    
            'create new list box item for every call
            For Each callNode As Xml.XmlNode In dateNode.SelectNodes("/date/call")
                'create root item
                Dim callItem As New ListViewItem
                callItem.Text = dateStamp
    
                'add sub items
                callItem.SubItems.Add(callNode.SelectSingleNode("time").InnerText)
                callItem.SubItems.Add(callNode.SelectSingleNode("account").InnerText)
                callItem.SubItems.Add(callNode.SelectSingleNode("product").InnerText)
                callItem.SubItems.Add(callNode.SelectSingleNode("issue").InnerText)
                callItem.SubItems.Add(callNode.SelectSingleNode("resolution").InnerText)
                listView.Items.Add(callItem)
            Next
    
        End Sub
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Iterating XML Data to a Listview

    Oh, wow, ok. That's a lot for me to digest. Let me read this over because I'm not just going to copy and paste, I need to understand it. I'll get back here as soon as it clicks. Thanks so much for your help

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Iterating XML Data to a Listview

    So I've spent the last day fixing my compiler so that it saves the XML data the way you suggested. I had not worked with attributes before so this was a little tough, but I got it. As far as the example you provided I think I understand it. You're iterating to get each node name and pass it to a sub you created that takes that particular node and grabs its data and adds it to the listview in a way that makes sense. The issue I'm having is that I'm not understanding exactly how it's getting the correct node names and passing them to your sub. No matter how I do it, when I get to your sub, datenode always returns as system.xml.xmlelement. Isn't this supposed to be one of the nodes from the iteration by the time it gets to this sub? So that it can use that data from that node and drop it in? Another thing I don't understand is how we're throwing terms around like "datenode" and "callnode" without declaring them.

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: Iterating XML Data to a Listview

    I got it! Wildbill got it right, he just anticipated the way I wanted to order the data and that's what tripped me up a little bit. Everything works PERFECTLY now! Thank you so much!

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