Get WSDL object into a SQL Database

前端 未结 1 1080
感情败类
感情败类 2021-01-29 12:46

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

相关标签:
1条回答
  • 2021-01-29 13:06

    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.

    0 讨论(0)
提交回复
热议问题