Click here to Skip to main content
16,004,678 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hello all, i was wondering how i could read through a xml document.
The structure of the xml is as follows...
XML
<response>
 <forecast>
  <txt_forecast>
  </txt_forecast>
  <simpleforecast>
   <forecastdays>
    <forecastday>
      <day>tuesday</day>
      <tempF>100</tempF>
      <tempC>40</tempC>
    </forecastday>
    <forecastday>
       <day>...
       <tempF>
        .....
    </forecastday>
   </forecastdays>
  </simpleforecast>
 </forecast>
</response>


now i have selected the forecastdays node which is of main concern to me... like
C#
XmlDocument xmlForecast = new XmlDocument();
        xmlForecast.Load(string.Format("http://adfm.com/api/aasdfadf3/forecast/q/np/{0}", strCityName));

if (xmlForecast.SelectSingleNode("/response/error") != null)
            {
                condition.Error = xmlForecast.SelectSingleNode("/response/error/description").InnerText;
            }
            else if(xmlForecast.SelectSingleNode("/response/forecast/simpleforecast/forecastdays")!=null)
            {

                
                
            }


now all i want to do is iterate through all the forecastday nodes present inside the
response/forecast/simpleforecast/forecastdays....and store it in a generic list.
How do i do the iteration?? i just want to know how to iterate...after that storing a dictionary or a list<string> will be no problem.

Thanks in advance,
Minghang
Posted

1 solution

use below for loop to iterate through forecastdays to get each forecastday
C#
else if (xmlForecast.SelectSingleNode("/response/forecast/simpleforecast/forecastdays") != null)
            {
                XmlNodeList forecastdays = xmlForecast.SelectSingleNode("/response/forecast/simpleforecast/forecastdays").ChildNodes;
                foreach (XmlNode forecastday in forecastdays)
                {

                }
            }
 
Share this answer
 
Comments
Minghang 23-Oct-12 13:23pm    
Thank you hitesh! That is exactly what i wanted...although i have to put in lots of iterations inside since the xml actually contains much more nodes. However, the code that u provided worked for me. Thanks again! Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900