Take the 2-minute tour ×

I'm trying to retrieve XML data that looks like this:

<server id="256698330093839" name="Blackstar Games Server" address="blackstar.xs4all.nl"
    port="6774" num_users="13" version="1.2" start="1286488744">        
... more records snipped ...

with this code:

            WebClient client = new WebClient();
            var url = "http://www.openrpg.com/openrpg_servers.php";
            var s = client.DownloadString(url);
            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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer


