读取XML时C#嵌套If语句

读取XML时C#嵌套If语句,c#,if-statement,nested-loops,C#,If Statement,Nested Loops,我正在阅读以下格式的XML天气文档- <Rep D="ENE" F="1" G="9" H="81" Pp="9" S="7" T="3" V="VG" W="7" U="0">0</Rep> // 00:00 <Rep D="NE" F="0" G="9" H="86" Pp="1" S="4" T="3" V="VG" W="2" U="0">180</Rep> //03:00 <Rep D="NNE" F="0" G="9" H="87

我正在阅读以下格式的XML天气文档-

<Rep D="ENE" F="1" G="9" H="81" Pp="9" S="7" T="3" V="VG" W="7" U="0">0</Rep> // 00:00
<Rep D="NE" F="0" G="9" H="86" Pp="1" S="4" T="3" V="VG" W="2" U="0">180</Rep> //03:00
<Rep D="NNE" F="0" G="9" H="87" Pp="4" S="4" T="2" V="GO" W="7" U="0">360</Rep> //06:00
<Rep D="NNE" F="2" G="7" H="81" Pp="8" S="4" T="4" V="VG" W="7" U="1">540</Rep> //09:00
<Rep D="NNE" F="5" G="7" H="72" Pp="4" S="2" T="6" V="VG" W="7" U="2">720</Rep> //12:00
<Rep D="N" F="6" G="7" H="69" Pp="4" S="2" T="7" V="VG" W="7" U="1">900</Rep> //15:00
<Rep D="NW" F="5" G="4" H="77" Pp="5" S="2" T="6" V="VG" W="7" U="1">1080</Rep> //18:00
<Rep D="N" F="2" G="9" H="88" Pp="8" S="4" T="4" V="VG" W="8" U="0">1260</Rep> //21:00
如下

       string RepValueZero = elemList[0].InnerText;
       string RepValueZero = elemList[0].InnerText;

        //Get the Number of Minutes in the day to calculate which row to read
        double mins = DateTime.Now.TimeOfDay.TotalMinutes;

        if (mins > Convert.ToInt32(RepValueZero) && mins < Convert.ToInt32(RepValueOne))
        {
            //Get Temp and Weather Value From Row [0]
            string T = elemList[0].Attributes["T"].Value;
            string W = elemList[0].Attributes["W"].Value;

            if(W = 1)
            {
              image1.ImageUrl = "w1.jpg";
            }
            if(W = 2)
            {
              image1.ImageUrl = "w2.jpg";
            }
            ...
            ...
            if(W = 30)
            {
              image1.ImageUrl = "w3.jpg";
            }
        }
string RepValueZero=elemList[0]。InnerText;
字符串RepValueZero=elemList[0]。InnerText;
//获取一天中计算要读取的行的分钟数
双分钟=DateTime.Now.TimeOfDay.TotalMinutes;
if(分钟>转换为32(RepValueZero)和&min<转换为32(RepValueOne))
{
//从第[0]行获取温度和天气值
字符串T=elemList[0]。属性[“T”]。值;
字符串W=elemList[0]。属性[“W”]。值;
如果(W=1)
{
image1.ImageUrl=“w1.jpg”;
}
如果(W=2)
{
image1.ImageUrl=“w2.jpg”;
}
...
...
如果(W=30)
{
image1.ImageUrl=“w3.jpg”;
}
}

您可以替换此:

        if(W = 1)
        {
          image1.ImageUrl = "w1.jpg";
        }
        if(W = 2)
        {
          image1.ImageUrl = "w2.jpg";
        }
        ...
为此:

image1.ImageUrl = "w" + W + ".jpg";
或者,如果每个数字都有不同的图像,可以执行以下操作:

Dictionary<int, string> jpgsById = new Dictionary<int, string> ();
jpgsById[1] = "w1.jpg";
jpgsById[2] = "w2.jpg";
image1.ImageUrl = jpgsById[1];
Dictionary jpgsById=newdictionary();
jpgsById[1]=“w1.jpg”;
jpgsById[2]=“w2.jpg”;
image1.ImageUrl=jpgsById[1];

您可以替换此:

        if(W = 1)
        {
          image1.ImageUrl = "w1.jpg";
        }
        if(W = 2)
        {
          image1.ImageUrl = "w2.jpg";
        }
        ...
为此:

image1.ImageUrl = "w" + W + ".jpg";
或者,如果每个数字都有不同的图像,可以执行以下操作:

Dictionary<int, string> jpgsById = new Dictionary<int, string> ();
jpgsById[1] = "w1.jpg";
jpgsById[2] = "w2.jpg";
image1.ImageUrl = jpgsById[1];
Dictionary jpgsById=newdictionary();
jpgsById[1]=“w1.jpg”;
jpgsById[2]=“w2.jpg”;
image1.ImageUrl=jpgsById[1];

您可以创建30个继承自Weather类的类,并具有一个WeatherFactory模式,该模式根据字符串参数返回特定的WeatherSubclass

abstract class Weather
{
 public string ImageUrl{get;set;};
}

class MildWeather : Weather
{
   public MildWeather()
  {
      ImageUrl = "your specific image for MildWeather";
  } 
}
在你的代码中有这样的东西

string T = elemList[0].Attributes["T"].Value;
string W = elemList[0].Attributes["W"].Value;
var weather = WeatherFactory.Create(W);

image1.ImageUrl = weather.ImageUrl;

因此,如果您想添加另一个子类,因为在xml中会出现另一个值W,那么您只需要从Weather类继承并指定该类型的图像,并且您的模型将可以扩展这些更改。您不需要添加额外的if。

您可以创建30个继承自Weather类的类,并具有一个WeatherFactory模式,该模式根据字符串参数返回特定的WeatherSubclass

abstract class Weather
{
 public string ImageUrl{get;set;};
}

class MildWeather : Weather
{
   public MildWeather()
  {
      ImageUrl = "your specific image for MildWeather";
  } 
}
在你的代码中有这样的东西

string T = elemList[0].Attributes["T"].Value;
string W = elemList[0].Attributes["W"].Value;
var weather = WeatherFactory.Create(W);

image1.ImageUrl = weather.ImageUrl;

因此,如果您想添加另一个子类,因为在xml中会出现另一个值W,那么您只需要从Weather类继承并指定该类型的图像,并且您的模型将可以扩展这些更改。您不需要添加额外的if。

如果您的问题是关于工作代码的,那么请显示一些属于的问题,并清楚地说明您遇到的问题。目前你的问题还不清楚。希望这能让它更清楚一点-所以我现在把所有的东西都写在页面上,这样我就可以测试,确保我根据标签的内部文本和当前时间阅读了正确的标签。我省略了所有七个if语句,只使用了一个if和else。谢谢如果
ImageUrl
基于
W
的值,那么您可以使用
W
构建您的URL,并且不需要
if
s。否则,我将使用
开关
语句。OK-我已经使用image1.ImageURL=“w”+w+“.jpg”构建了URL;这很好,但是如果我想使用W的文本值,即0=多云,1=晴天,2=雷电等。您能在if语句中提供一个开关示例吗?谢谢如果你的问题是关于工作代码的,那么请展示一些有问题的东西,并清楚地说明你的问题。目前你的问题还不清楚。希望这能让它更清楚一点-所以我现在把所有的东西都写在页面上,这样我就可以测试,确保我根据标签的内部文本和当前时间阅读了正确的标签。我省略了所有七个if语句,只使用了一个if和else。谢谢如果
ImageUrl
基于
W
的值,那么您可以使用
W
构建您的URL,并且不需要
if
s。否则,我将使用
开关
语句。OK-我已经使用image1.ImageURL=“w”+w+“.jpg”构建了URL;这很好,但是如果我想使用W的文本值,即0=多云,1=晴天,2=雷电等。您能在if语句中提供一个开关示例吗?ThanksGreat-如您所述构建URL对于图像URL是有效的。我不知道我该如何使用这本词典(我以前从未使用过)。如果我想使用W的描述,我如何将其合并到我的代码中,即0=晴天,1=阴天,2=部分阴天等。感谢描述,查找枚举。很好-像您描述的那样构建URL对图像URL很有效。我不知道我该如何使用这本词典(我以前从未使用过)。如果我想使用W的描述,我如何将其合并到我的代码中,即0=晴天,1=多云,2=部分多云等。感谢描述,查找枚举。