I have the following xml file to give an idea of layout,
<weather>
<current_conditions>
<condition data="Mostly Cloudy" />
<temp_f data="48" />
<temp_c data="9" />
<humidity data="Humidity: 71%" />
<icon data="/ig/images/weather/mostly_cloudy.gif" />
<wind_condition data="Wind: W at 17 mph" />
</current_conditions>
<forecast_conditions>
<day_of_week data="Sun" />
<low data="34" />
<high data="48" />
<icon data="/ig/images/weather/mostly_sunny.gif" />
<condition data="Partly Sunny" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Mon" />
<low data="32" />
<high data="45" />
<icon data="/ig/images/weather/sunny.gif" />
<condition data="Clear" />
</forecast_conditions>
I am learning c# and want to export to csv using a row for each day of the week, where info for a day is not available i need to show no data in the csv row. I appreciate the simplest solution as i am learning :)
I'm assuming you want to ignore the current conditions node as it doesn't make much sense to me to have a csv file where the columns don't all match the same data. For getting all the forecast info into a csv you can use:
XDocument doc = XDocument.Parse(...);
StringBuilder sb = new StringBuilder(1000);
foreach (XElement node in doc.Descendants("forecast_conditions"))
{
foreach(XElement innerNode in node.Elements())
{
sb.AppendFormat("{0},", innerNode.Attribute("data").Value);
}
//Remove trailing comma
sb.Remove(sb.Length - 1, 1);
sb.AppendLine();
}
File.WriteAllText(@"c:\temp.csv", sb.ToString());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.