는 XML에
라이브 예를 Linq에를 사용하여 응답을 구문 분석 할 수있는 방법입니다 http://balexandre.com/stackoverflow/7789623/이
가
이 링크는 소스 코드를 포함, 그러나 LINQ to XML의를 사용하여 구문 분석 빠르고 쉬운이다 , 재미와 매우 간단 :
private GoogleWheatherInfo parseGoogleWeatherResponse(string url)
{
GoogleWheatherInfo gw = new GoogleWheatherInfo();
// get the XML
XDocument doc = XDocument.Load(url);
// parse data
gw.ForecastInformation = (from x in doc.Descendants("forecast_information")
select new GWForecastInfo
{
City = x.Descendants("city").First().Attribute("data").Value,
PostalCode = x.Descendants("postal_code").First().Attribute("data").Value,
Latitude = long.Parse(string.IsNullOrEmpty(x.Descendants("latitude_e6").First().Attribute("data").Value) ? "0" : x.Descendants("latitude_e6").First().Attribute("data").Value),
Longitude = long.Parse(string.IsNullOrEmpty(x.Descendants("longitude_e6").First().Attribute("data").Value) ? "0" : x.Descendants("longitude_e6").First().Attribute("data").Value),
ForecastDate = DateTime.ParseExact(x.Descendants("forecast_date").First().Attribute("data").Value, "yyyy-MM-dd", CultureInfo.InvariantCulture),
CurrentDate = DateTime.ParseExact(x.Descendants("current_date_time").First().Attribute("data").Value, "yyyy-MM-dd HH:mm:ss K", CultureInfo.InvariantCulture),
UnitSystem = x.Descendants("unit_system").First().Attribute("data").Value
}).Single();
gw.CurrentCondition = (from x in doc.Descendants("current_conditions")
select new GWCurrentCondition
{
Condition = x.Descendants("condition").First().Attribute("data").Value,
TemperatureC = long.Parse(x.Descendants("temp_c").First().Attribute("data").Value),
TemperatureF = long.Parse(x.Descendants("temp_f").First().Attribute("data").Value),
Humidity = x.Descendants("humidity").First().Attribute("data").Value,
Image = x.Descendants("icon").First().Attribute("data").Value,
Wind = x.Descendants("wind_condition").First().Attribute("data").Value
}).Single();
gw.ForecastConditions = (from x in doc.Descendants("forecast_conditions")
select new GWForecastCondition
{
DayOfWeek = x.Descendants("day_of_week").First().Attribute("data").Value,
Low = double.Parse(x.Descendants("low").First().Attribute("data").Value),
High = double.Parse(x.Descendants("high").First().Attribute("data").Value),
Image = x.Descendants("icon").First().Attribute("data").Value,
Condition = x.Descendants("condition").First().Attribute("data").Value,
}).ToList();
return gw;
}
나는이 당신에게 어떤 XML 문서를 구문 분석하는 것이 얼마나 쉬운에 대한 몇 가지 지식을 제공하겠습니다.
현재 직면 한 문제는 무엇입니까? – V4Vendetta
현재 웹 서비스에서 객체를 가져 왔지만 객체를 구문 분석 할 수 없습니다. – wyo9089
질문이 명확하지 않습니다. 어떤 Google 날씨 서비스를 사용하고 있는지 알려주는 자세한 정보를 제공하십시오. – gprasant