简体   繁体   中英

C# Nested If Statements whilst reading XML

I am reading a XML Weather document which is in the format of below -

<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

The text value of the Rep element is related to the time in minutes.

To ensure I am reading the correct element am using an if statement I am storing the innerText of the Rep element as a variable then working out how many mins have been and comparing this value to the innerText to see which line I should be reading

   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 strTemp = elemList[0].Attributes["T"].Value;
        string strWeatherType = elemList[0].Attributes["W"].Value;
    }

... and so on.

After getting the values of 'W' - which is the weather type I then want to display a image. (But there are 30 possible values for 'W') How can i achieve this without having a nested if statement of 30 branches within each initial if statement (above)

if(W = 1)
{
  image1.ImageUrl = "w1.jpg";
}
if(W = 2)
{
  image1.ImageUrl = "w2.jpg";
}
...
...
if(W = 30)
{
  image1.ImageUrl = "w3.jpg";
}

Like below

       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";
            }
        }

You could replace this:

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

with this:

image1.ImageUrl = "w" + W + ".jpg";

Alternatively if you have a different image for each number, you can do something like this:

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

You can create 30 classes that inherit from Weather class, and have a WeatherFactory pattern that returns the specific WeatherSubclass depending on the string parameter.

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

class MildWeather : Weather
{
   public MildWeather()
  {
      ImageUrl = "your specific image for MildWeather";
  } 
}

and have in your code something like this

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

image1.ImageUrl = weather.ImageUrl;

So in case you want to add another Subclass because in your xml appears another value of W, just you need to do is inherit from Weather class and specify the image for that type, and your model will be extensible for those changes. You will not need to add an extra if.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM