Search code examples
c#asp.netsqlwsdl

Get WSDL object into a SQL Database


So I hate to ask this but the last 10 hours of searching and attempting to code have turned up nothing.

I have a visual studio project with an SQL database attached. I need to pull data from a google weather service API into the sql table.

The webservice call is to this site google api call

As well as a couple other sites like accuweather and NOAA to show the differences between the three. The projects goal is to see whether the data is relatively the same or if different weather stations are bring used, and if so does it cause a significant difference in the way the weather is reported to the user.

What I'm missing is an interpreter for the object can allow me to put the temp, humidity ect into a sql table

Has anyone done this before and have some tips or references?


Solution

  • Here is how you can parse the response using Linq to XML

    Live Example: http://balexandre.com/stackoverflow/7789623/

    The link also include the source code, but the parsing using Linq to XML is quick easy, fun and extremely simple:

    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;
    }
    

    I hope this gives you some knowledge on how easy is to parse any XML document.