Program.cs
using ConsoleApp2.Models;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string City_Name = "GdaĆsk";
var doc = XElement.Load($"https://maps.googleapis.com/maps/api/place/textsearch/xml?key={key}&query=restaurants+in+{City_Name}&language=pl&fields=place_id");
List<PlaceIdModel> IdList = doc.Descendants("result").Select(n => new PlaceIdModel
{
PlaceId = n.Element("place_id").Value
}).ToList();
List<PlaceDetailsModel> placeDetailsModel = new List<PlaceDetailsModel>();
foreach (var item in IdList)
{
var doc2 = XElement.Load($"https://maps.googleapis.com/maps/api/place/details/xml?key={key}&place_id={item.PlaceId}&language=pl&fields=name,vicinity,geometry/location,rating,website,opening_hours/weekday_text");
placeDetailsModel = doc2.Descendants("result").Select(x => new PlaceDetailsModel
{
Name = x.Element("name").Value,
Vicinity = x.Element("vicinity").Value,
Rating = x.Element("rating").Value,
Website = x.Element("website").Value,
Lat = x.Element("geometry").Element("location").Element("lat").Value,
Lng = x.Element("geometry").Element("location").Element("lng").Value,
Opening_hours = x.Descendants("weekday_text").Select(y => y.Value).ToList()
}).ToList();
};
}
}
}
PlaceIdModel.cs
namespace ConsoleApp2.Models
{
public class PlaceIdModel
{
public string PlaceId { get; set; }
}
}
PlaceDetailsModel.cs
using System.Collections.Generic;
namespace ConsoleApp2.Models
{
public class PlaceDetailsModel
{
public string Name { get; set; }
public string Vicinity { get; set; }
public string Rating { get; set; }
public string Website { get; set; }
public string Lat { get; set; }
public string Lng { get; set; }
public List<string> Opening_hours { get; set; }
}
}
I'm using goole API places. First I get place_id from city then I want to use loop foreach to save details of every place into list. Everytime everything save on [0] instead populate whole list.