简体   繁体   English

在 c# 中解析天气预报数据(来自 NDFD)

[英]Parse Weather Forecast Data (from the NDFD) in c#

I am using > http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl webservice to get the weather detials by calling GmlTimeSeries webmethod.我正在使用 > http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl网络服务通过调用 GmlTimeSeries 网络方法获取天气详细信息。 Now I just want to read temparature, weather icon link details from the xml.现在我只想阅读 xml 中的温度、天气图标链接详细信息。 The xml has huge data. xml 拥有庞大的数据。 Can anybody give an idea to fetch the required data from the xml?任何人都可以提出从 xml 获取所需数据的想法吗?

NDFD HOme Page NDFD主页

The XML looks almost like below: Full XML File is Here XML 看起来几乎如下所示: 完整的 XML 文件在这里

I want to fetch Temparature from below xml data:我想从 xml 数据下方获取温度:

 <gml:featureMember>
          <app:Forecast_Gml2Point>
             <gml:position>
                <gml:Point srsName="EPSG:4326">
                   <gml:coordinates>-87.8859170,41.7450495</gml:coordinates>
                </gml:Point>
             </gml:position>
             <app:validTime>2011-06-07T12:00:00</app:validTime>
             <app:temperature>77.0</app:temperature>
          </app:Forecast_Gml2Point>
       </gml:featureMember>

       <gml:featureMember>
          <app:Forecast_Gml2Point>
             <gml:position>
                <gml:Point srsName="EPSG:4326">
                   <gml:coordinates>-87.8859170,41.7450495</gml:coordinates>
                </gml:Point>
             </gml:position>
             <app:validTime>2011-06-07T15:00:00</app:validTime>
             <app:temperature>90.0</app:temperature>
          </app:Forecast_Gml2Point>
       </gml:featureMember>

And weather phrase from below:和下面的天气短语:

 <gml:featureMember>
      <app:Forecast_Gml2Point>
         <gml:position>
            <gml:Point srsName="EPSG:4326">
               <gml:coordinates>-87.8859170,41.7450495</gml:coordinates>
            </gml:Point>
         </gml:position>
         <app:validTime>2011-06-08T03:00:00</app:validTime>
         <app:weatherPhrase>Mostly Clear</app:weatherPhrase>
      </app:Forecast_Gml2Point>
   </gml:featureMember>

   <gml:featureMember>
      <app:Forecast_Gml2Point>
         <gml:position>
            <gml:Point srsName="EPSG:4326">
               <gml:coordinates>-87.8859170,41.7450495</gml:coordinates>
            </gml:Point>
         </gml:position>
         <app:validTime>2011-06-08T06:00:00</app:validTime>
         <app:weatherPhrase>Mostly Clear</app:weatherPhrase>
      </app:Forecast_Gml2Point>
   </gml:featureMember>

The above is a piece of xml file.以上是一段xml文件。 Like this I have large data for 7 days weather details.像这样,我有 7 天天气详细信息的大数据。 I need to read for the temp and weather condition from above xml.我需要从 xml 上方读取温度和天气状况。

Full XML File is Here 完整的 XML 文件在这里

I think you will find your answer here我想你会在这里找到你的答案

Edit: you need to use namespace, for example:编辑:您需要使用命名空间,例如:

XNamespace app = "http://www.weather.gov/forecasts/xml/OGC_services";
var result = from i in doc.Descendants(app+"Forecast_Gml2Point")
                  select new 
                  {
                      temperature = i.Element(app + "temperature"), 
                      icon = i.Element(app+"weatherIcon")
                  };

Edit 2: if you need to get Element with other namespace, here is another example:编辑 2:如果您需要使用其他命名空间获取 Element,这是另一个示例:

XNamespace gml ="http://www.opengis.net/gml"
i.Element(gml+"coordinates" )

It would be easier if you use the "Add Web Reference" feature of Visual Studio.如果使用 Visual Studio 的“添加 Web 参考”功能会更容易。 In this way, Visual Studio generates all the (proxy) classes for you base on the WSDL, and you can then program against the classes like how you'll do normally.通过这种方式,Visual Studio 会根据 WSDL 为您生成所有(代理)类,然后您就可以像平常一样对这些类进行编程。 In other words, there is no parsing of XML needed.换句话说,不需要解析 XML。

As pointed out in this link :如此链接中指出的那样:

Visual Studio.Net Web References are proxy classes created on the client to connect to the Web Service running on the server. Visual Studio.Net Web 引用是在客户端创建的代理类,用于连接到服务器上运行的 Web 服务。 Inside of the IDE Web references automatically generate code and insert hidden files into your project.在 IDE Web 内部引用会自动生成代码并将隐藏文件插入到您的项目中。 This is required because.Net is type safe and in order to compile code that uses the Web Service, the client has to know the method signature of each method that is called.这是必需的,因为 .Net 是类型安全的,并且为了编译使用 Web 服务的代码,客户端必须知道调用的每个方法的方法签名。

You might want to refer to the above link about consuming WSDL in details.您可能想详细参考上面关于使用 WSDL 的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM