Results 1 to 5 of 5

Thread: [RESOLVED] ListView/XML Problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    17

    Resolved [RESOLVED] ListView/XML Problem

    Hello other friendly developers!
    I've got a little frustrating problem, because I can't find the problem in my code:

    1: I've got a form containing one control (ListView1, a ListView control)

    2: I've got this XML File (WebSites.xml)
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <WebSites>
      <WebSite>
        <Name>ASP.NET</Name>
        <URL>http://www.asp.net</URL>
        <Info>Hierzo Info</Info>
      </WebSite>
      <WebSite>
        <Name>Windows Forms</Name>
        <URL>http://www.windowsforms.net</URL>
        <Info>All about Windows Forms</Info>
      </WebSite>
      <WebSite>
        <Name>GotDotNet</Name>
        <URL>http://www.gotdotnet.com</URL>
        <Info>GotDotNet Programme</Info>
      </WebSite>
    </WebSites>
    3: I've got this VisualBasic.NET2 code:
    Code:
    Dim rdrXML As New Xml.XmlTextReader("WebSites.xml")
            rdrXML.MoveToContent()
    
            Dim ElementName As String = ""
            Dim objListViewItem As ListViewItem = Nothing
    
            Do While rdrXML.Read
                objListViewItem = New ListViewItem
    
                ListView1.Items.Add(objListViewItem)
                If rdrXML.NodeType = 1 Then
                    ElementName = rdrXML.Name
                ElseIf rdrXML.NodeType = 3 Then
                    If ElementName = "Name" Then
                        objListViewItem.Text = rdrXML.Value
                    End If
                    If ElementName = "URL" Then
                        objListViewItem.SubItems.Add(rdrXML.Value.ToString)
                    End If
                    If ElementName = "Info" Then
                        objListViewItem.SubItems.Add(rdrXML.Value.ToString)
                    End If
                End If
            Loop
            rdrXML.Close()
    However, I get this strange result. I want all the properties in one row (For each website a row containing Name, URL and Info).

    Can anyone help me? This really makes my head feel like exploding!
    Attached Images Attached Images  
    Last edited by DumbAndDumber; Dec 5th, 2009 at 03:19 PM. Reason: Removed a confusing line of code unneeded

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

    Re: ListView/XML Problem

    vb Code:
    1. Dim rdrXML As New Xml.XmlTextReader("WebSites.xml")
    2. rdrXML.MoveToContent()
    3.  
    4. Dim ElementName As String = ""
    5. Dim objListViewItem As ListViewItem = Nothing
    6.  
    7. Do While rdrXML.Read
    8.     If rdrXML.NodeType = 1 Then
    9.         ElementName = rdrXML.Name
    10.     ElseIf rdrXML.NodeType = 3 Then
    11.         If ElementName = "Name" Then
    12.             objListViewItem = New ListViewItem(rdrXML.Value)
    13.         End If
    14.         If ElementName = "URL" Then
    15.             objListViewItem.SubItems.Add(rdrXML.Value.ToString)
    16.         End If
    17.         If ElementName = "Info" Then
    18.             objListViewItem.SubItems.Add(rdrXML.Value.ToString)
    19.         End If
    20.         ListView1.Items.Add(objListViewItem)
    21.     End If
    22. Loop
    23. rdrXML.Close()

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    17

    Re: ListView/XML Problem

    Thanks, .paul. for your post, however, I get the following exception (translated):
    Code:
    System.ArgumentException was unhandled
    Unable to add ASP.NET on more than one place. Remove the item from the current location or copy it.
    ParamName="item"
    Source="System.Windows.Forms"
    Form1.vb:line 21
    P.S.: ASP.NET is a node in the XML file (to prevent confusion).
    Last edited by DumbAndDumber; Dec 5th, 2009 at 03:19 PM.
    If my post helped you out, please rate me!
    Thread resolved? Mark it as Resolved!

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

    Re: ListView/XML Problem

    try this:

    vb Code:
    1. Dim doc As New Xml.XmlDocument
    2. doc.Load("WebSites.xml")
    3.  
    4. For Each node As Xml.XmlNode In doc.SelectNodes("WebSites/WebSite")
    5.     Dim objListViewItem As New ListViewItem(node.SelectSingleNode("Name").InnerText)
    6.     objListViewItem.SubItems.Add(node.SelectSingleNode("URL").InnerText)
    7.     objListViewItem.SubItems.Add(node.SelectSingleNode("Info").InnerText)
    8.     ListView1.Items.Add(objListViewItem)
    9. Next

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    17

    Re: ListView/XML Problem

    Thanks a lot! Very short, simple piece of code but it works very well! Thanks once again
    If my post helped you out, please rate me!
    Thread resolved? Mark it as Resolved!

Tags for this Thread

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