I am trying to get the date from "forecaseDateLocalStr" and put it in the new DateTime(result). But I am getting an object error. I want to format the date and add the new xml element as a child for each dailyForecastPeriod. This is in my handler in c#, there is a sample of xml and code below.
string day = xFore.Root.Element("forecastDateLocalStr").Value;
Int32.TryParse(day, out result);
string dayfilename = string.Format("yyyy,MM,dd", result);
DateTime dateValue = new DateTime(result);
var d = dateValue.DayOfWeek.ToString();
xFore.Root.Add(
new XElement("day", d));
>
`<forecast>
<dailyForecastPeriods>
<cloudCoverPercent>2</cloudCoverPercent>
<dewPoint>62.6</dewPoint>
<iconCode>65</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>54</relativeHumidity>
<summaryDescription>Very Hot</summaryDescription>
<temperature>90.1</temperature>
<thunderstormProbability>0</thunderstormProbability>
<windDirectionDegrees>211</windDirectionDegrees>
<windSpeed>8.9</windSpeed>
<detailedDescription>Mostly sunny. Hot with highs around 90. South winds around 5 mph...Increasing to 10 to 15 mph this afternoon. </detailedDescription>
<forecastDateLocalStr>2014-07-22T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-22T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>39</cloudCoverPercent>
<dewPoint>66.2</dewPoint>
<iconCode>106</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>64</relativeHumidity>
<summaryDescription>40% Chance of Storms</summaryDescription>
<temperature>72.1</temperature>
<thunderstormProbability>0</thunderstormProbability>
<windDirectionDegrees>241</windDirectionDegrees>
<windSpeed>8.9</windSpeed>
<detailedDescription>Partly cloudy in the evening...Then mostly cloudy with a chance of showers and thunderstorms after midnight. Humid with lows in the lower 70s. Southwest winds 5 to 10 mph. Chance of rain 40 percent. </detailedDescription>
<forecastDateLocalStr>2014-07-22T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-22T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>75</cloudCoverPercent>
<dewPoint>62.6</dewPoint>
<iconCode>95</iconCode>
<precipCode>1</precipCode>
<precipProbability>45</precipProbability>
<relativeHumidity>70</relativeHumidity>
<summaryDescription>50% Chance of Storms</summaryDescription>
<temperature>76.9</temperature>
<thunderstormProbability>50</thunderstormProbability>
<windDirectionDegrees>336</windDirectionDegrees>
<windSpeed>8.9</windSpeed>
<detailedDescription>Partly sunny with a chance of showers and thunderstorms. Not as warm with highs in the upper 70s. Northwest winds 10 to 15 mph with gusts up to 25 mph. Chance of rain 50 percent. </detailedDescription>
<forecastDateLocalStr>2014-07-23T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-23T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>37</cloudCoverPercent>
<dewPoint>53.6</dewPoint>
<iconCode>2</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>62</relativeHumidity>
<summaryDescription>Partly Cloudy</summaryDescription>
<temperature>58.9</temperature>
<thunderstormProbability>0</thunderstormProbability>
<windDirectionDegrees>17</windDirectionDegrees>
<windSpeed>6.7</windSpeed>
<detailedDescription>Mostly cloudy in the evening...Then becoming partly cloudy. Cooler with lows in the upper 50s. North winds 5 to 10 mph with gusts up to 20 mph. </detailedDescription>
<forecastDateLocalStr>2014-07-23T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-23T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>2</cloudCoverPercent>
<dewPoint>50</dewPoint>
<iconCode>7</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>52</relativeHumidity>
<summaryDescription>Sunny</summaryDescription>
<temperature>76.9</temperature>
<thunderstormProbability>0</thunderstormProbability>
<windDirectionDegrees>16</windDirectionDegrees>
<windSpeed>6.7</windSpeed>
<detailedDescription>Sunny. Highs in the mid 70s. North winds 5 to 10 mph</detailedDescription>
<forecastDateLocalStr>2014-07-24T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-24T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>42</cloudCoverPercent>
<dewPoint>51.8</dewPoint>
<iconCode>70</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>61</relativeHumidity>
<summaryDescription>Mostly Clear</summaryDescription>
<temperature>55</temperature>
<thunderstormProbability>0</thunderstormProbability>
<windDirectionDegrees>241</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Mostly clear. Lows in the mid 50s. </detailedDescription>
<forecastDateLocalStr>2014-07-24T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-24T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>86</cloudCoverPercent>
<dewPoint>53.6</dewPoint>
<iconCode>7</iconCode>
<precipCode>1</precipCode>
<precipProbability>10</precipProbability>
<relativeHumidity>55</relativeHumidity>
<summaryDescription>Sunny</summaryDescription>
<temperature>79.1</temperature>
<thunderstormProbability>10</thunderstormProbability>
<windDirectionDegrees>220</windDirectionDegrees>
<windSpeed>6.7</windSpeed>
<detailedDescription>Sunny. Highs in the upper 70s. </detailedDescription>
<forecastDateLocalStr>2014-07-25T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-25T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>65</cloudCoverPercent>
<dewPoint>57.2</dewPoint>
<iconCode>2</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>64</relativeHumidity>
<summaryDescription>Partly Cloudy</summaryDescription>
<temperature>60</temperature>
<thunderstormProbability>40</thunderstormProbability>
<windDirectionDegrees>210</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Partly cloudy. Lows around 60. </detailedDescription>
<forecastDateLocalStr>2014-07-25T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-25T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>58</cloudCoverPercent>
<dewPoint>60.8</dewPoint>
<iconCode>93</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>64</relativeHumidity>
<summaryDescription>30% Chance of Storms</summaryDescription>
<temperature>78</temperature>
<thunderstormProbability>30</thunderstormProbability>
<windDirectionDegrees>113</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Partly sunny with a chance of showers and thunderstorms. Highs in the upper 70s. Chance of rain 30 percent</detailedDescription>
<forecastDateLocalStr>2014-07-26T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-26T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>58</cloudCoverPercent>
<dewPoint>64.4</dewPoint>
<iconCode>106</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>76</relativeHumidity>
<summaryDescription>40% Chance of Storms</summaryDescription>
<temperature>65.9</temperature>
<thunderstormProbability>0</thunderstormProbability>
<windDirectionDegrees>97</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Mostly cloudy with a chance of showers and thunderstorms. Lows in the mid 60s. Chance of rain 40 percent. </detailedDescription>
<forecastDateLocalStr>2014-07-26T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-26T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>31</cloudCoverPercent>
<dewPoint>62.6</dewPoint>
<iconCode>94</iconCode>
<precipCode>1</precipCode>
<precipProbability>30</precipProbability>
<relativeHumidity>66</relativeHumidity>
<summaryDescription>40% Chance of Storms</summaryDescription>
<temperature>82</temperature>
<thunderstormProbability>50</thunderstormProbability>
<windDirectionDegrees>334</windDirectionDegrees>
<windSpeed>2.2</windSpeed>
<detailedDescription>Partly sunny with a chance of showers and thunderstorms. Highs in the lower 80s. Chance of rain 40 percent</detailedDescription>
<forecastDateLocalStr>2014-07-27T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-27T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>79</cloudCoverPercent>
<dewPoint>60.8</dewPoint>
<iconCode>106</iconCode>
<precipCode>1</precipCode>
<precipProbability>40</precipProbability>
<relativeHumidity>70</relativeHumidity>
<summaryDescription>40% Chance of Storms</summaryDescription>
<temperature>64</temperature>
<thunderstormProbability>60</thunderstormProbability>
<windDirectionDegrees>233</windDirectionDegrees>
<windSpeed>6.7</windSpeed>
<detailedDescription>Mostly cloudy with a chance of showers and thunderstorms. Lows in the mid 60s. Chance of rain 40 percent. </detailedDescription>
<forecastDateLocalStr>2014-07-27T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-27T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>59</cloudCoverPercent>
<dewPoint>59</dewPoint>
<iconCode>94</iconCode>
<precipCode>1</precipCode>
<precipProbability>55</precipProbability>
<relativeHumidity>70</relativeHumidity>
<summaryDescription>40% Chance of Storms</summaryDescription>
<temperature>79.1</temperature>
<thunderstormProbability>40</thunderstormProbability>
<windDirectionDegrees>296</windDirectionDegrees>
<windSpeed>11.2</windSpeed>
<detailedDescription>Partly sunny with a chance of showers and thunderstorms. Highs in the upper 70s. Chance of rain 40 percent</detailedDescription>
<forecastDateLocalStr>2014-07-28T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-28T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>18</cloudCoverPercent>
<dewPoint>57.2</dewPoint>
<iconCode>188</iconCode>
<precipCode>1</precipCode>
<precipProbability>55</precipProbability>
<relativeHumidity>73</relativeHumidity>
<summaryDescription>60% Chance of Rain</summaryDescription>
<temperature>59.5</temperature>
<thunderstormProbability>20</thunderstormProbability>
<windDirectionDegrees>315</windDirectionDegrees>
<windSpeed>8.9</windSpeed>
<detailedDescription>Mostly clear with a chance of rain. Chance of precipitation 55%. Low temperature around 60F. Dew point will be around 57F with an average humidity of 73%. Winds will be 9 mph from the NW.</detailedDescription>
<forecastDateLocalStr>2014-07-28T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-28T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>48</cloudCoverPercent>
<dewPoint>53.6</dewPoint>
<iconCode>3</iconCode>
<precipCode>1</precipCode>
<precipProbability>0</precipProbability>
<relativeHumidity>63</relativeHumidity>
<summaryDescription>Partly Cloudy</summaryDescription>
<temperature>71.1</temperature>
<thunderstormProbability>0</thunderstormProbability>
<windDirectionDegrees>300</windDirectionDegrees>
<windSpeed>8.9</windSpeed>
<detailedDescription>Partly cloudy. High temperature around 71F. Dew point will be around 54F with an average humidity of 63%. Winds will be 9 mph from the WNW.</detailedDescription>
<forecastDateLocalStr>2014-07-29T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-29T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>62</cloudCoverPercent>
<dewPoint>53.6</dewPoint>
<iconCode>189</iconCode>
<precipCode>1</precipCode>
<precipProbability>65</precipProbability>
<relativeHumidity>72</relativeHumidity>
<summaryDescription>70% Chance of Rain</summaryDescription>
<temperature>57.9</temperature>
<thunderstormProbability>10</thunderstormProbability>
<windDirectionDegrees>256</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Partly cloudy with probable rain. Chance of precipitation 65%. Low temperature around 58F. Dew point will be around 54F with an average humidity of 72%. Winds will be 4 mph from the WSW.</detailedDescription>
<forecastDateLocalStr>2014-07-29T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-29T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>75</cloudCoverPercent>
<dewPoint>53.6</dewPoint>
<iconCode>202</iconCode>
<precipCode>1</precipCode>
<precipProbability>75</precipProbability>
<relativeHumidity>66</relativeHumidity>
<summaryDescription>80% Chance of Storms</summaryDescription>
<temperature>72</temperature>
<thunderstormProbability>40</thunderstormProbability>
<windDirectionDegrees>205</windDirectionDegrees>
<windSpeed>6.7</windSpeed>
<detailedDescription>Mostly cloudy with a 75% chance for rain and thunderstorms. High temperature around 72F. Dew point will be around 54F with an average humidity of 66%. Winds will be 7 mph from the SSW.</detailedDescription>
<forecastDateLocalStr>2014-07-30T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-30T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>80</cloudCoverPercent>
<dewPoint>55.4</dewPoint>
<iconCode>214</iconCode>
<precipCode>1</precipCode>
<precipProbability>75</precipProbability>
<relativeHumidity>71</relativeHumidity>
<summaryDescription>80% Chance of Storms</summaryDescription>
<temperature>59.4</temperature>
<thunderstormProbability>30</thunderstormProbability>
<windDirectionDegrees>261</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Mostly cloudy with a 75% chance for rain and thunderstorms. Low temperature around 59F. Dew point will be around 55F with an average humidity of 71%. Winds will be 4 mph from the W.</detailedDescription>
<forecastDateLocalStr>2014-07-30T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-30T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>72</cloudCoverPercent>
<dewPoint>53.6</dewPoint>
<iconCode>194</iconCode>
<precipCode>1</precipCode>
<precipProbability>60</precipProbability>
<relativeHumidity>61</relativeHumidity>
<summaryDescription>60% Chance of Rain</summaryDescription>
<temperature>73.9</temperature>
<thunderstormProbability>20</thunderstormProbability>
<windDirectionDegrees>332</windDirectionDegrees>
<windSpeed>6.7</windSpeed>
<detailedDescription>Mostly cloudy with a chance of rain. Chance of precipitation 60%. High temperature around 74F. Dew point will be around 54F with an average humidity of 61%. Winds will be 7 mph from the NNW.</detailedDescription>
<forecastDateLocalStr>2014-07-31T07:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-31T11:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>false</isNightTimePeriod>
</dailyForecastPeriods>
<dailyForecastPeriods>
<cloudCoverPercent>22</cloudCoverPercent>
<dewPoint>55.4</dewPoint>
<iconCode>188</iconCode>
<precipCode>1</precipCode>
<precipProbability>60</precipProbability>
<relativeHumidity>70</relativeHumidity>
<summaryDescription>60% Chance of Rain</summaryDescription>
<temperature>60.8</temperature>
<thunderstormProbability>20</thunderstormProbability>
<windDirectionDegrees>269</windDirectionDegrees>
<windSpeed>4.5</windSpeed>
<detailedDescription>Mostly clear with a chance of rain. Chance of precipitation 60%. Low temperature around 61F. Dew point will be around 55F with an average humidity of 70%. Winds will be 4 mph from the W.</detailedDescription>
<forecastDateLocalStr>2014-07-31T19:00:00</forecastDateLocalStr>
<forecastDateUtcStr>2014-07-31T23:00:00Z</forecastDateUtcStr>
<isNightTimePeriod>true</isNightTimePeriod>
</dailyForecastPeriods>
<forecastCreatedUtcStr>2014-07-22T12:01:00Z</forecastCreatedUtcStr>
<location>US39T0014</location>
<locationType>city</locationType>
</forecast>
>
`
string forecastUrl = "https://thepulseapi.earthnetworks.com/data/forecasts/v1/daily?location=" + lat + "," + lon + "&locationtype=latitudelongitude&units=english&cultureinfo=en-en>&verbose=true&access_token=" + token;
var source = client.DownloadString(forecastUrl);
JObject jo = JObject.Parse(source);
XNode xNode = JsonConvert.DeserializeXNode(jo.ToString(), "forecast");
xFore = XDocument.Parse(xNode.ToString());
DateTime dateValue;
DateTime.TryParse(xFore.Root.Element("forecastDateLocalStr").Value, out dateValue);
string weekDay = dateValue.DayOfWeek.ToString();
xFore.Root.Add(new XElement("day", weekDay));
Given that it looks like you're using LINQ to XML, you don't need to do any of the parsing manually. Just use the conversions to DateTime
:
XElement forecastDateElement = xFore.Root.Element("forecastDateLocalStr");
DateTime forecastDate = (DateTime) forecastDateElement;
(If you're responsible for the XML format, I'd strongly encourage you to change the element name, by the way...)
I suspect you actually want to change how you find the element though, as forecastDateLocalStr
is under dailyForecastPeriods
. So you may want:
XElement forecastDateElement = xFore.Root
.Element("dailyForecastPeriods")
.Element("forecastDateLocalStr");
That will find the first forecastDateLocalStr
element within the first dailyForecastPeriods
element. If you want more than that, you might want to use Elements
or Descendants
...
It's not clear what you're trying to do afterwards, although yyyy,MM,dd
is a very strange date format (not one that I've ever seen before). You should use something like:
string text = forecastDate.ToString("yyyy,MM,dd", CultureInfo.InvariantCulture);
if you really want to format that though...
See more on this question at Stackoverflow