Reputation: 33465
I'm trying to retrieve XML data that looks like this:
<servers>
<server id="256698330093839" name="Blackstar Games Server" address="blackstar.xs4all.nl"
port="6774" num_users="13" version="1.2" start="1286488744">
</server>
... more records snipped ...
</servers>
with this code:
WebClient client = new WebClient();
var url = "http://www.openrpg.com/openrpg_servers.php";
Program.log(url);
var s = client.DownloadString(url);
Program.log(s);
var xml = XElement.Parse(s);
var servers =
from e in xml.Descendants("servers")
select new Server
{
port = (int)e.Attribute("port"),
users = (int)e.Attribute("num_users"),
name = (string)e.Attribute("name"),
address = (string)e.Attribute("address"),
};
Gets as far as successfully parsing the data into the element tree in xml
, doesn't throw any exceptions, but when it's done, servers
is empty instead of containing the list of servers. Can anyone see what I'm doing wrong?
Upvotes: 0
Views: 262
Reputation: 244948
You want to get the sequence of all elements with the name server
, not servers
, so you have to use
from e in xml.Descendants("server")
Also, I would recommend using Elements()
instead of Descendants()
if you want to retrieve only direct children.
Upvotes: 1