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

发布于 2024-11-14 15:47:04 字数 4864 浏览 6 评论 0原文

我正在使用> http://www.weather.gov/forecasts/xml/DWMLgen/wsdl /ndfdXML.wsdl Web服务通过调用GmlTimeSeries Web方法获取天气详细信息。现在我只想从 xml 中读取温度、天气图标链接详细信息。 xml数据量很大。任何人都可以提供从 xml 中获取所需数据的想法吗?

NDFD 主页

XML 看起来几乎如下所示:完整的 XML 文件在这里

我想从下面的 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>

以及从下面的天气短语:

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

上面是一段 xml 文件。像这样我有 7 天天气详细信息的大数据。我需要从上面的 xml 中读取温度和天气状况。

<一href="http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLclient.php?whichClient=GmlLatLonList&lat=&lon=&listLatLon=&a mp;lat1=&lon1=&lat2=&lon2=&resolutionSub=&listLat1=&listLon1=&listLat2=&listLon2=&resolutionList=&a mp;endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&listEndPoint1Lat=&listEndPoint1Lon=&listEndPoint2La t=&listEndPoint2Lon=&zipCodeList=&listZipCodeList=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon =&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSq uare=&citiesLevel=&listCitiesLevel=&扇区=&gmlListLatLon=38.99,-77.02%2039.70,-104.80%2047.6,-122.30&featureType=F orecast_Gml2Point&requestedTime=&startTime=2000-01-01T00:00:00&endTime=2012-01-01T00:00:00&compType=Between&proper tyName=wx,temp,icons&product=时间序列&begin=2004-01-01T00:00:00&end=2015-06-07T00:00:00&maxt=maxt&Submit=提交" rel="nofollow">完整的 XML 文件在这里

I am using > http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl webservice to get the weather detials by calling GmlTimeSeries webmethod. Now I just want to read temparature, weather icon link details from the xml. The xml has huge data. Can anybody give an idea to fetch the required data from the xml?

NDFD HOme Page

The XML looks almost like below : Full XML File is Here

I want to fetch Temparature from below xml data:

 <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. Like this I have large data for 7 days weather details. I need to read for the temp and weather condition from above xml.

Full XML File is Here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性 2024-11-21 15:47:04

我想你会找到答案 此处

编辑:
您需要使用名称空间,例如:

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

编辑2:
如果您需要获取具有其他名称空间的 Element,这里是另一个示例:

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

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:

XNamespace gml ="http://www.opengis.net/gml"
i.Element(gml+"coordinates" )
﹏半生如梦愿梦如真 2024-11-21 15:47:04

如果使用 Visual Studio 的“添加 Web 引用”功能会更容易。通过这种方式,Visual Studio 会根据 WSDL 为您生成所有(代理)类,然后您可以像平常一样针对这些类进行编程。换句话说,不需要解析 XML。

正如此链接中指出的:

Visual Studio.Net Web 引用是在客户端上创建的代理类,用于连接到在服务器上运行的 Web 服务。 IDE 内部的 Web 引用会自动生成代码并将隐藏文件插入到您的项目中。这是必需的,因为 .Net 是类型安全的,并且为了编译使用 Web 服务的代码,客户端必须知道所调用的每个方法的方法签名。

您可能需要参考上述链接了解有关使用 WSDL 的详细信息。

It would be easier if you use the "Add Web Reference" feature of Visual Studio. 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. In other words, there is no parsing of XML needed.

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. Inside of the IDE Web references automatically generate code and insert hidden files into your project. 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.

You might want to refer to the above link about consuming WSDL in details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文