harry.luson harry.luson - 1 month ago 9
C# Question

How to get title of landmark place from Wikipedia by API?

For my code I am using the Wikipedia API which provides links to all places that are linked with the Wikipedia article of that city. But with my code there are some extra unnecessary links. I want to only return links where the type is "landmark".

My Wikipedia API is:

https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type|name|dim|country|region|globe&format=json


Sample JSON data from the Wikipedia API:

"query": {
"geosearch": [
{
"pageid": 5858187,
"ns": 0,
"title": "Stuttgart Hauptbahnhof",
"lat": 48.783888888889,
"lon": 9.1816666666667,
"dist": 136.8,
"primary": "",
"type": "railwaystation",
"name": "",
"dim": 1000,
"country": "DE",
"region": "BW"
},
{
"pageid": 6102287,
"ns": 0,
"title": "Staatstheater Stuttgart",
"lat": 48.780277777778,
"lon": 9.185,
"dist": 361,
"primary": "",
"type": "landmark",
"name": "",
"dim": "900",
"country": "DE",
"region": "BW"
},
{
"pageid": 35806545,
"ns": 0,
"title": "Versatel building",
"lat": 48.78409,
"lon": 9.17799,
"dist": 400.4,
"primary": "",
"type": null,
"name": "",
"dim": 1000,
"country": null,
"region": null
},
{
"pageid": 3230957,
"ns": 0,
"title": "Neue Staatsgalerie",
"lat": 48.780277777778,
"lon": 9.1869444444444,
"dist": 430.6,
"primary": "",
"type": "landmark",
"name": "",
"dim": 1000,
"country": "DE",
"region": "BW"
},
....
]
}


My code to get the
Title
from this API.

using (var client = new HttpClient())
{
var response = client.GetAsync("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type|name|dim|country|region|globe&format=json").Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
string responseString = responseContent.ReadAsStringAsync().Result;
var obj = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch.Select(a => a.title).ToList();

foreach (var item in obj)
{
Console.WriteLine(item);
}
}
}


Here is the current output:

Output

How can I get the title from the results where the
type
is "landmark"?

Answer

If you use JSON format, try this:

var obj = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch
    .Where(a => a.type == "landmark").Select(a => a.title).ToList();

Also you can get all titles without to use Json.NET. This is how I do it with XML format:

using (var webResponse = (HttpWebResponse)WebRequest.Create("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type&format=xml").GetResponse())
{
    using (var reader = new StreamReader(webResponse.GetResponseStream()))
    {
        var response = XElement.Parse(reader.ReadToEnd());
        var obj = response.Descendants("gs")
            .Where(a => a.Attribute("type") != null && a.Attribute("type").Value == "landmark")
            .Select(a => a.Attribute("title").Value).ToList();
    }
}