-
Nov 19th, 2012, 01:35 PM
#1
Thread Starter
Lively Member
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 .
-
Nov 19th, 2012, 02:15 PM
#2
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); } } }
Last edited by .paul.; Nov 19th, 2012 at 02:21 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 02:31 PM
#3
Thread Starter
Lively Member
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
-
Nov 19th, 2012, 02:42 PM
#4
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#?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 02:44 PM
#5
Re: Read XML and add it to a listView with multi columns
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 02:44 PM
#6
Thread Starter
Lively Member
Re: Read XML and add it to a listView with multi columns
C# am I on the wrong forum , sorry .
-
Nov 19th, 2012, 05:26 PM
#7
Thread Starter
Lively Member
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");
-
Nov 19th, 2012, 05:50 PM
#8
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 05:56 PM
#9
Thread Starter
Lively Member
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 .
-
Nov 19th, 2012, 05:59 PM
#10
Re: Read XML and add it to a listView with multi columns
must be an invalid xml file then...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 06:30 PM
#11
Thread Starter
Lively Member
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>
-
Nov 19th, 2012, 06:40 PM
#12
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"));
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 06:44 PM
#13
Thread Starter
Lively Member
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.
-
Nov 19th, 2012, 06:45 PM
#14
Re: Read XML and add it to a listView with multi columns
is app path your bin/debug folder?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 06:47 PM
#15
Thread Starter
Lively Member
Re: Read XML and add it to a listView with multi columns
-
Nov 19th, 2012, 06:49 PM
#16
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");
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 06:53 PM
#17
Thread Starter
Lively Member
Re: Read XML and add it to a listView with multi columns
gives me the same error .
-
Nov 19th, 2012, 06:55 PM
#18
Re: Read XML and add it to a listView with multi columns
check your bin/debug folder. does servers.xml exist there?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 06:57 PM
#19
Thread Starter
Lively Member
Re: Read XML and add it to a listView with multi columns
-
Nov 19th, 2012, 06:59 PM
#20
Re: Read XML and add it to a listView with multi columns
is your exe file in that folder too?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2012, 07:01 PM
#21
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|