如何获取xml中的所有其他元素(iconCode)

时间:2014-08-11 12:19:29

标签: c# xml

第一个dailyForecastPeriods用于预测日期,第二个用于夜间预测,依此类推。我想循环并得到奇数iconCode,1,3,5,7等等。在C#下面我有一个foreach循环,它抓取iconCode的每个实例,但结果它只显示xml中的最后一个iconCode。我需要帮助将每个iconCode应用为字符串,格式如下,并将其添加到xml以应用于xsl工作表。您可以在代码末尾看到我在xml中添加的内容。

C#:

         XDocument scan = XDocument.Parse(xNode.ToString());
         foreach (XElement element in scan.Descendants("iconCode"))
                {
                  iconCode1 = element.Value;
                }

         Int32 result;                             
         Int32.TryParse(iconCode1, out result);
         string iconFilename2 = "cond" + string.Format("{0:00#}", result) + ".png";

         xFore.Root.Add(new XElement("img_small", weatherImages + @"Small/" + iconFilename2),
         new XElement("img_large", weatherImages + @"Large/" + iconFilename2));

XML:

<forecast>
   <dailyForecastPeriods>
      <iconCode>3</iconCode>
      <summaryDescription>Partly Cloudy</summaryDescription>
      <temperature>81.1</temperature>
      <detailedDescription>Partly cloudy. Highs in the lower 80s. East winds 5 to 10 mph.      </detailedDescription>
   </dailyForecastPeriods>
   <dailyForecastPeriods>
      <iconCode>2</iconCode>
     <summaryDescription>Partly Cloudy</summaryDescription>
     <temperature>63</temperature>
     <detailedDescription>Partly cloudy. Lows in the lower 60s. East winds 5 to 10 mph.   </detailedDescription>
   </dailyForecastPeriods>
   <dailyForecastPeriods>
      <iconCode>3</iconCode>
      <summaryDescription>Partly Cloudy</summaryDescription>
      <temperature>82</temperature>
      <detailedDescription>Partly cloudy. Highs in the lower 80s. East winds 10 to 15 mph. </detailedDescription>
   </dailyForecastPeriods>
   <dailyForecastPeriods>
      <iconCode>2</iconCode>
      <summaryDescription>Partly Cloudy</summaryDescription>
      <temperature>65</temperature>
      <detailedDescription>Partly cloudy. Lows in the mid 60s. Northeast winds 10 to 15 mph. </detailedDescription>
   </dailyForecastPeriods>
   <dailyForecastPeriods>
      <iconCode>3</iconCode>
      <summaryDescription>Partly Cloudy</summaryDescription>
      <temperature>82</temperature>
      <detailedDescription>Partly cloudy. Highs in the lower 80s. Northeast winds 5 to 10 mph. </detailedDescription>
   </dailyForecastPeriods>
   <dailyForecastPeriods>
      <iconCode>73</iconCode>
      <summaryDescription>Mostly Cloudy</summaryDescription>
      <temperature>67</temperature>
      <detailedDescription>Mostly cloudy. Lows in the mid 60s. </detailedDescription></dailyForecastPeriods>
   <dailyForecastPeriods>
      <iconCode>93</iconCode>
      <summaryDescription>30% Chance of Storms</summaryDescription>
      <temperature>82</temperature>
      <detailedDescription>Partly sunny with a chance of showers and thunderstorms. Highs in the lower 80s. Chance of rain 30 percent</detailedDescription>
   </dailyForecastPeriods>
   <dailyForecastPeriods>
      <iconCode>105</iconCode>
      <summaryDescription>30% Chance of Storms</summaryDescription>
      <temperature>64</temperature>
      <detailedDescription>Mostly cloudy with a chance of showers and thunderstorms. Lows in the mid 60s. Chance of rain 30 percent. </detailedDescription>
   </dailyForecastPeriods>
   <dailyForecastPeriods>
      <iconCode>95</iconCode>
      <summaryDescription>50% Chance of Storms</summaryDescription
<temperature>81.1</temperature>
    <detailedDescription>Partly sunny with a chance of showers and thunderstorms. Highs in the lower 80s. Chance of rain 50 percent</detailedDescription>
    </dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>2</iconCode>
    <summaryDescription>Partly Cloudy</summaryDescription>
    <temperature>61.1</temperature>
    <detailedDescription>Partly cloudy. Lows in the lower 60s. </detailedDescription></dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>3</iconCode>
    <summaryDescription>Partly Cloudy</summaryDescription>
    <temperature>78</temperature>
    <detailedDescription>Partly cloudy. Highs in the upper 70s. </detailedDescription></dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>2</iconCode>
    <summaryDescription>Partly Cloudy</summaryDescription>
    <temperature>58</temperature>
    <detailedDescription>Partly cloudy. Lows in the upper 50s. </detailedDescription></dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>3</iconCode>
    <summaryDescription>Partly Cloudy</summaryDescription>
    <temperature>78</temperature>
    <detailedDescription>Partly cloudy. Highs in the upper 70s. </detailedDescription></dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>70</iconCode>
    <summaryDescription>Mostly Clear</summaryDescription>
    <temperature>56.3</temperature>
    <detailedDescription>Mostly clear. Low temperature around 56F. Dew point will be around 50F with an average humidity of 61%. Winds will be 7 mph from the WNW.</detailedDescription>
    </dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>26</iconCode>
    <summaryDescription>Mostly Sunny</summaryDescription>
    <temperature>74.5</temperature>
    <detailedDescription>Mostly sunny. High temperature around 74F. Dew point will be around 50F with an average humidity of 53%. Winds will be 9 mph from the WNW.</detailedDescription>
    </dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>31</iconCode>
    <summaryDescription>Clear</summaryDescription>
    <temperature>60.4</temperature>
    <detailedDescription>Clear. Low temperature around 60F. Dew point will be around 52F with an average humidity of 63%. Winds will be 4 mph from the NNW.</detailedDescription></dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>7</iconCode>
    <summaryDescription>Sunny</summaryDescription>
    <temperature>76.3</temperature>
    <detailedDescription>Sunny. High temperature around 76F. Dew point will be around 54F with an average humidity of 57%. Winds will be 4 mph from the NW.</detailedDescription></dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>31</iconCode>
    <summaryDescription>Clear</summaryDescription>
    <temperature>62.1</temperature>
    <detailedDescription>Clear. Low temperature around 62F. Dew point will be around 55F with an average humidity of 65%. Winds will be 2 mph from the WSW.</detailedDescription></dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>3</iconCode>
    <summaryDescription>Partly Cloudy</summaryDescription>
    <temperature>76.6</temperature>
    <detailedDescription>Partly cloudy. High temperature around 77F. Dew point will be around 55F with an average humidity of 59%. Winds will be 4 mph from the W.</detailedDescription></dailyForecastPeriods>
    <dailyForecastPeriods>
    <iconCode>70</iconCode>
    <summaryDescription>Mostly Clear</summaryDescription>
    <temperature>62.2</temperature>
    <detailedDescription>Mostly clear. Low temperature around 62F. Dew point will be around 55F with an average humidity of 65%. Winds will be 4 mph from the WSW.</detailedDescription>
    </dailyForecastPeriods>
    <img_small>https://hylantnet.hylant.com/WeatherImages/Small/cond070.png</img_small>     

https://hylantnet.hylant.com/WeatherImages/Large/cond070.png

1 个答案:

答案 0 :(得分:1)

这是正常的,当您浏览所有元素并将它们存储在同一个变量中时,只会处理最后一个元素。因此,每个值都会覆盖前一个值。

如果你想避免这种行为,你可以在foreach循环中执行你的治疗,就像这样:

foreach (XElement element in scan.Descendants("iconCode"))
{
     string iconCode1 = element.Value;
     Int32 result;                             
     Int32.TryParse(iconCode1, out result);
     string iconFilename2 = "cond" + string.Format("{0:00#}", result) + ".png";

     xFore.Root.Add(new XElement("img_small", weatherImages + @"Small/" + iconFilename2),
     new XElement("img_large", weatherImages + @"Large/" + iconFilename2));
}