2011-06-07 41 views
1

我正在使用>http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl webservice通過調用GmlTimeSeries webmethod獲取天氣情況。現在我只想從xml中讀取溫度,天氣圖標鏈接的詳細信息。 XML有巨大的數據。任何人都可以提供一個想法從XML獲取所需的數據?解析c#中的天氣預報數據(來自NDFD)#

NDFD HOme Page

的XML看起來幾乎象下面這樣:Full XML File is Here

我想從下面的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讀取溫度和天氣狀況。

Full XML File is Here

+0

我試着用XDocument.Parse()方法解析xml。但無法閱讀'' – 2011-06-07 11:37:38

回答

3

我想你會發現你的答案here

編輯: 你需要使用命名空間,例如:如果你需要得到元素 :

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與其他命名空間,這裏是另一個例子:

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

我嘗試了在給定鏈接中解釋的方式,但無法像這樣讀取 – 2011-06-07 12:23:44

+0

顯示您嘗試的代碼。 – Reniuz 2011-06-07 12:35:33

+0

**'var getTemp = from i in XDocument.Parse(strResult).Descendants(「Forecast_Gml2Point」)select i.Element(「temperature」); '** ...我得到'枚舉沒有結果'錯誤 – 2011-06-07 12:39:54

1

如果使用Visual Studio的「添加Web引用」功能將會更容易。通過這種方式,Visual Studio可以基於WSDL爲您生成所有(代理)類,然後您可以針對類進行編程,例如您將如何正常執行。換句話說,不需要解析XML。

正如this link指出:

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

您可能想要參考the above link有關詳細消耗WSDL的內容。

+0

我只以這種方式使用web服務。這裏web服務的webmethod給出了我在問題中提供的XML格式的響應。 – 2011-06-07 13:25:43

+0

如果是這樣的話,我建議你使用LINQ to XML,如果你使用的.Net版本支持它...... LINQ to XML提供了有用的類,比如XElement,它允許你快速解析XML文檔... http:// msdn.microsoft.com/en-us/library/bb299195(v=VS.100).aspx ... – wooncherk 2011-06-07 14:36:58

+0

看看@Reniuz給出的答案。你會得到我想要的 – 2011-06-07 14:43:21