1

With My code I can Generate Lots of Places name from a Wikipedia article. Suppose if I look for Flensburg wikipedia page it will give all the external page links of places as name. So at this moment all the places are shown on output as list form like

Maasbüll
Bov Municipality
Hürup
Hürup(Amt)
Kupfermühle
.........and so on...

Now what I want to do is, I want to store all these places paired with its city name. Suppose here Flensburg is the city name. So i want to store it as following way-

Flensburg;Maasbüll;Bov Municipality;Hürup;Hürup(Amt);Kupfermühle.... so on..

My code to generate list of all places are as follows-

 using (var client = new HttpClient())
        {
            var response = client.GetAsync("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Flensburg&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);
                }

             }


        }

I want to know how can I store my data like I mentioned.

harry.luson
  • 247
  • 7
  • 19
  • @J... Could you please describe a ittle bit more how to proceed. I am very handling this case. Thank you – harry.luson Feb 23 '16 at 10:24
  • Harry, does my answer solve your problem? – NoName Feb 23 '16 at 10:58
  • @Sakura: Thank you very much, Now it is working. And could you please remove your another answer. Because may be it would be confusing for some other one. I added this two line below the console output. Please add this 2 line in your code also. as the question is to store the data in storage. Thank you very much for you time.var output = query + ";" + cities[query]; File.WriteAllText(@"C:\C# Visual Studio\City.txt", output); – harry.luson Feb 23 '16 at 11:01

4 Answers4

2
using System.Collections.Generic;

Code:

Dictionary<string, string> cities = new Dictionary<string, string>();
string query = "Flensburg";
using (var client = new HttpClient())
{
    var response = client.GetAsync("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=" + WebUtility.UrlEncode(query) + "&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();

        List<string> places = new List<string>();
        foreach (var item in obj)
        {
             places.Add(item);
        }
        cities[query] = string.Join(";", places);


       Console.WriteLine(query + ":" + cities[query]);
       var output = query + ";" + cities[query];
       File.WriteAllText(@"C:\C# Visual Studio\City.txt", output);
     }


}
NoName
  • 7,940
  • 13
  • 56
  • 108
  • Your code only shows the City name in alist not the Places name – harry.luson Feb 23 '16 at 10:30
  • I tried your code. but it gives a place name first and city name after that. Like Maasbüll Flensburg in this way, and it continued for every places. But I wante to to keep the city name at first and then all the places with ; basis. It is totally different – harry.luson Feb 23 '16 at 10:35
  • Yes I changed it But it shows output as Flensburg before every palces name ans also as list format not according to my expected result. there are 500 places and Flensburg is added before every place. – harry.luson Feb 23 '16 at 10:41
  • Everything with ; interval – harry.luson Feb 23 '16 at 10:44
  • it shows error- Error 5 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement for cities[query] in line Console.WriteLine(query + ": " cities[query]); – harry.luson Feb 23 '16 at 10:51
  • Thank you very much, Now it is working. And could you please remove your another answer. Because may be it would be confusing for some other one. I added this two line below the console output. Please add this 2 line in your code also. as the question is to store the data in storage. Thank you very much for you time.var output = query + ";" + cities[query]; File.WriteAllText(@"C:\C# Visual Studio\City.txt", output); – harry.luson Feb 23 '16 at 10:59
0

As per your own tag, A Dictionary will do exactly what you want.

Dictionary<string, Object> cities = new Dictionary<string, Object>();
cities.Add(name, item);
Ward D.S.
  • 536
  • 3
  • 11
0

Do this -

var output = String.Join(";", obj );
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24
  • I tried your code, and it joint every places with ;. Could you please give me hints how can I add City name before it – harry.luson Feb 23 '16 at 10:43
  • I have put item insted of object. Because while trying with object it does not give the correct answer. But after putting the item the string joint does not work.foreach (var item in obj) { var output = String.Join(";", item); Console.WriteLine(output); } – harry.luson Feb 23 '16 at 10:48
  • there is no `City` element in your `JSON` – Amit Kumar Ghosh Feb 23 '16 at 10:55
0
Dictionary<cityname,List<associated places>> d = new Dictionary<cityname,List<associated places>>();

key will be the city name that you search and value is a list which contains all the associated places

Add multiple places as values in a list with a single key of a dictionary.

vivek verma
  • 1,716
  • 1
  • 17
  • 26