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 .
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:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("ip")
ListView1.Columns.Add("server")
ListView1.Columns.Add("country")
Dim loadDoc As New System.Xml.XmlDocument()
loadDoc.Load(Application.StartupPath + "\servers.xml")
For Each ipNode As System.Xml.XmlNode In loadDoc.SelectNodes("/users/profile")
Dim item As ListViewItem = ListView1.Items.Add(ipNode.Attributes("ip").InnerText)
item.SubItems.Add(ipNode.Attributes("server").InnerText)
item.SubItems.Add(ipNode.Attributes("country").InnerText)
Next
End Sub
End Class
c# Code:
public class Form1
{
private void Form1_Load(System.Object sender, System.EventArgs e)
{
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);
}
}
}
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
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#?
1 Attachment(s)
Re: Read XML and add it to a listView with multi columns
here it is in c#:
Attachment 93415
Re: Read XML and add it to a listView with multi columns
C# am I on the wrong forum , sorry .
Re: Read XML and add it to a listView with multi columns
I use your code like this :
Quote:
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");
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?
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 .
Re: Read XML and add it to a listView with multi columns
must be an invalid xml file then...
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>
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"));
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.
Re: Read XML and add it to a listView with multi columns
is app path your bin/debug folder?
Re: Read XML and add it to a listView with multi columns
Re: Read XML and add it to a listView with multi columns
no, no, no:D save it like this:
myWebClient.DownloadFile(myStringWebResource, Application.StartupPath + "\\servers.xml");
Re: Read XML and add it to a listView with multi columns
gives me the same error .
Re: Read XML and add it to a listView with multi columns
check your bin/debug folder. does servers.xml exist there?
Re: Read XML and add it to a listView with multi columns
Re: Read XML and add it to a listView with multi columns
is your exe file in that folder too?
Re: Read XML and add it to a listView with multi columns
yes it is , both are in Debug folder.