簡體   English   中英

在 c# 中解析天氣預報數據(來自 NDFD)

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

我正在使用 > http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl網絡服務通過調用 GmlTimeSeries 網絡方法獲取天氣詳細信息。 現在我只想閱讀 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 上方讀取溫度和天氣狀況。

完整的 XML 文件在這里

我想你會在這里找到你的答案

編輯:您需要使用命名空間,例如:

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

如果使用 Visual Studio 的“添加 Web 參考”功能會更容易。 通過這種方式,Visual Studio 會根據 WSDL 為您生成所有(代理)類,然后您就可以像平常一樣對這些類進行編程。 換句話說,不需要解析 XML。

如此鏈接中指出的那樣:

Visual Studio.Net Web 引用是在客戶端創建的代理類,用於連接到服務器上運行的 Web 服務。 在 IDE Web 內部引用會自動生成代碼並將隱藏文件插入到您的項目中。 這是必需的,因為 .Net 是類型安全的,並且為了編譯使用 Web 服務的代碼,客戶端必須知道調用的每個方法的方法簽名。

您可能想詳細參考上面關於使用 WSDL 的鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM