Search code examples
c#xmlweather-api

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


Solution

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