【问题标题】:Get WSDL object into a SQL Database将 WSDL 对象放入 SQL 数据库
【发布时间】:2011-12-09 00:44:45
【问题描述】:

所以我不想问这个问题,但过去 10 个小时的搜索和尝试编码都没有结果。

我有一个附加了 SQL 数据库的 Visual Studio 项目。我需要将数据从谷歌天气服务 API 提取到 sql 表中。

网络服务调用是到这个站点google api call

还有一些其他网站,如 accuweather 和 NOAA,以显示三者之间的差异。该项目的目标是查看数据是否相对相同,或者是否使用了不同的气象站,如果是,是否会导致向用户报告天气的方式存在显着差异。

我缺少的是对象的解释器,可以让我将温度、湿度等放入 sql 表中

有没有人这样做过并有一些提示或参考?

【问题讨论】:

  • 您面临的问题是什么?
  • 目前我已经从网络服务中提取了对象,但我无法解析(可能不是正确的词)该对象。
  • 你的问题很不清楚。尝试提供更多信息以指出您正在使用的谷歌天气服务。

标签: c# asp.net sql wsdl


【解决方案1】:

这是使用 Linq to XML 解析响应的方法

实时示例: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 文档是多么容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-27
    • 2017-04-21
    • 2016-11-19
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多