Results 1 to 21 of 21

Thread: Read XML and add it to a listView with multi columns

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Question Read XML and add it to a listView with multi columns

    I use this code :

    Code:
    System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();
    loadDoc.Load(Application.StartupPath + "\\servers.xml");
    foreach (System.Xml.XmlNode ipNode in loadDoc.SelectNodes("/users/profile"))
            {
                listView1.Items.Add(ipNode.Attributes["ip"].InnerText); 
            }
    foreach (System.Xml.XmlNode serverNode in loadDoc.SelectNodes("/users/profile"))
            {
                listView1.Items[listView1.Items.Count - 1].SubItems.Add(serverNode.Attributes["server"].InnerText); 
            }
    foreach (System.Xml.XmlNode countryNode in loadDoc.SelectNodes("/users/profile"))
            {
                listView1.Items[listView1.Items.Count - 1].SubItems.Add(countryNode.Attributes["country"].InnerText);
            } 
        }
    and this is the xml :

    PHP Code:
    <?xml version="1.0"?>
    <users>
         <profile ip="212.23.122.45" server="server name1" country="D">
        </profile>
     <profile ip="212.22.123.122" server="server name2" country="D">
        </profile>
    </users>

    Nothing shows up in my listView , any help whould be great . Thank you .

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

    Re: Read XML and add it to a listView with multi columns

    first, the xml file must be in your app's bin/debug folder, then:

    vb.net 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.  
    5.         ListView1.View = View.Details
    6.         ListView1.Columns.Add("ip")
    7.         ListView1.Columns.Add("server")
    8.         ListView1.Columns.Add("country")
    9.  
    10.         Dim loadDoc As New System.Xml.XmlDocument()
    11.         loadDoc.Load(Application.StartupPath + "\servers.xml")
    12.         For Each ipNode As System.Xml.XmlNode In loadDoc.SelectNodes("/users/profile")
    13.             Dim item As ListViewItem = ListView1.Items.Add(ipNode.Attributes("ip").InnerText)
    14.             item.SubItems.Add(ipNode.Attributes("server").InnerText)
    15.             item.SubItems.Add(ipNode.Attributes("country").InnerText)
    16.         Next
    17.        
    18.     End Sub
    19.  
    20. End Class

    c# Code:
    1. public class Form1
    2. {
    3.  
    4.  
    5.     private void  Form1_Load(System.Object sender, System.EventArgs e)
    6.     {
    7.         ListView1.View = View.Details;
    8.         ListView1.Columns.Add("ip");
    9.         ListView1.Columns.Add("server");
    10.         ListView1.Columns.Add("country");
    11.  
    12.         System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();
    13.         loadDoc.Load(Application.StartupPath + "\\servers.xml");
    14.         foreach (System.Xml.XmlNode ipNode in loadDoc.SelectNodes("/users/profile")) {
    15.             ListViewItem item = ListView1.Items.Add(ipNode.Attributes["ip"].InnerText);
    16.             item.SubItems.Add(ipNode.Attributes["server"].InnerText);
    17.             item.SubItems.Add(ipNode.Attributes["country"].InnerText);
    18.         }
    19.  
    20.     }
    21.  
    22. }
    Last edited by .paul.; Nov 19th, 2012 at 02:21 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    I get this error :

    Code:
    Error	1	'System.Xml.XmlNode' does not contain a definition for 'Attributes' and the best extension method overload 'System.Xml.Linq.Extensions.Attributes(System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>, System.Xml.Linq.XName)' has some invalid arguments

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

    Re: Read XML and add it to a listView with multi columns

    this is the vb.net forum... which are you using vb.net or c#?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Read XML and add it to a listView with multi columns

    here it is in c#:

    csharp xml to listview.zip

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    C# am I on the wrong forum , sorry .

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    I use your code like this :

    string remoteUri = "http://website.com/";
    string fileName = "servers.xml", myStringWebResource = null;

    WebClient myWebClient = new WebClient();
    myStringWebResource = remoteUri + fileName;
    myWebClient.DownloadFile(myStringWebResource,fileName);

    listView1.View = View.Details;
    listView1.Columns.Add("ip");
    listView1.Columns.Add("server");
    listView1.Columns.Add("country");
    System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();
    loadDoc.Load(Application.StartupPath + "\\servers.xml");
    foreach (System.Xml.XmlNode ipNode in loadDoc.SelectNodes("/users/profile"))
    {
    ListViewItem item = listView1.Items.Add(ipNode.Attributes["ip"].InnerText);
    item.SubItems.Add(ipNode.Attributes["server"].InnerText);
    item.SubItems.Add(ipNode.Attributes["country"].InnerText);
    But I get error when I run the exe on line : loadDoc.Load(Application.StartupPath + "\\servers.xml");

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Read XML and add it to a listView with multi columns

    does the file exist at that time? i.e. has the download completed?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    yes it does I even add : System.Threading.Thread.Sleep(5000);
    before read xml .

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Read XML and add it to a listView with multi columns

    must be an invalid xml file then...

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    exactly how it looks :

    Code:
    <?xml version="1.0"?>
    <users>
         <profile ip="212.23.122.45" server="server name1" country="D">
        </profile>
     <profile ip="212.22.123.122" server="server name2" country="D">
        </profile>
    </users>

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Read XML and add it to a listView with multi columns

    ok. after:

    Code:
    myWebClient.DownloadFile(myStringWebResource,fileName);
    put:

    Code:
    Interaction.MsgBox(System.IO.File.Exists(Application.StartupPath + "\\servers.xml"));

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    this gives me error. is it not for to check if the xml file exists ? I have the xml file located in appath.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Read XML and add it to a listView with multi columns

    is app path your bin/debug folder?

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    no it is : obj\Release

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Read XML and add it to a listView with multi columns

    no, no, no save it like this:

    myWebClient.DownloadFile(myStringWebResource, Application.StartupPath + "\\servers.xml");

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    gives me the same error .

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Read XML and add it to a listView with multi columns

    check your bin/debug folder. does servers.xml exist there?

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    yes it exist.

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Read XML and add it to a listView with multi columns

    is your exe file in that folder too?

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Read XML and add it to a listView with multi columns

    yes it is , both are in Debug folder.

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