【问题标题】:How to make an if statement to show xml attribute based on current time如何根据当前时间制作 if 语句以显示 xml 属性
【发布时间】:2012-05-07 16:37:39
【问题描述】:

我有一个这样的 XML:

<PrayerTime
    Day ="1" 
    Month="5" 
    Fajr="07:00" 
    Sunrise="09:00"
    Zuhr="14:00"
/>

这样的类:

public class PrayerTime
{   
    public string Fajr { get; set; }
    public string Sunrise { get; set; }
    public string Zuhr { get; set; }
}

还有一些像这样获得价值的东西:

XDocument loadedCustomData = XDocument.Load("WimPrayerTime.xml");
var filteredData = from c in loadedCustomData.Descendants("PrayerTime")
                   where c.Attribute("Day").Value == myDay.Day.ToString()
                   && c.Attribute("Moth").Value == myDay.Month.ToString()
                   select new PrayerTime()
                   {
                       Fajr = c.Attribute("Fajr").Value,
                       Sunrise = c.Attribute("Sunrise").Value,
                   };
myTextBox.Text = filteredData.First().Fajr;

我如何根据当前时间说如果时间介于 Fajr 的值和 Sunrise 的值之间,那么 myTextBox 应该显示 Fajr 的值。 如果当前时间值在日出和Zuhr之间,显示Zuhr?

如何让它在 myTextBox2 中显示属性名称? 例如,myTextBox 显示值“07:00”,myTextBox2 显示“Fajr”?

【问题讨论】:

    标签: c# xml linq linq-to-xml datetime-comparison


    【解决方案1】:

    首先按照@abatischcev修改类

    public class PrayerTime
    {
        public TimeSpan Fajr { get; set; }
        public TimeSpan Sunrise { get; set; }
        public TimeSpan Zuhr { get; set; }
    }
    

    然后修改linq查询选择部分为:

    select new PrayerTime()
    {
        Fajr = TimeSpan.Parse(c.Attribute("Fajr").Value),
        Sunrise = TimeSpan.Parse(c.Attribute("Sunrise").Value),
        Zuhr = TimeSpan.Parse(c.Attribute("Zuhr").Value)
    };
    

    那么你的支票应该是:

    var obj = filteredData.First();
    TimeSpan currentTime = myDay.TimeOfDay;
    string result = String.Empty;
    if (currentTime >= obj.Fajr && currentTime < obj.Sunrise)
    {
        result = "Fajar";
    }
    else if (currentTime >= obj.Sunrise && currentTime < obj.Zuhr)
    {
        result = "Zuhar";
    }
    textbox1.Text = result;
    

    (顺便说一句,Zuhr 时间应该在 Zuhr 和 Asar 之间:))

    【讨论】:

      【解决方案2】:

      首先,不要保留字符串,而是保留 TimeSpan 对象:

      public TimeSpan Fajr { get; set; }
      public TimeSpan Sunrise { get; set; }
      

      为此,将 XML 解析为 DateTime:

      TimeSpan ts = TimeSpan.Parse(c.Attribute("attr"));
      

      所以:

      TimeSpan now = DateTime.Now.TimeOfDay; // time part only
      var data = filteredData.First();
      
      string result = null;
      if (data.Fajr <= now && now < data.Sunrise); // notice operators greed
          result = data.Fajr;
      else if (data.Sunrise <= now && now <= data.Zuhr)
          result = data.Zuhr;
      myTextBox.Text = result;
      

      【讨论】:

      • DateTime.LessThanOrEqual 没有字符串和 DateTime 的重载
      • @lukas:首先我误读了 OP 的问题,即他不是 DateTime 而是 TimeSpan 的字符串形式,所以后来我更新了我的答案。
      • @Habib.OSU:谢谢!确实是第一部分,当然,取决于 OP,他的逻辑,但听起来很公平。 2nd - 不确定,请自行尝试。
      【解决方案3】:

      这里的问题是您的代码是“字符串类型的”。我最好使用为时间设计的类型,例如DateTime,但要快速修复它:

                     // don't you need a third one here?
                     select new PrayerTime()
                     {
                         Fajr = c.Attribute("Fajr").Value,
                         Sunrise = c.Attribute("Sunrise").Value,
                     };
      

      获取当前时间:

      int currentHour = DateTime.Now.Hour;
      

      那么就是两个整数的简单比较。

      var data = filteredData.First();
      int fajrHour = int.Parse(data.Fajr.Substring(0, 2), CultureInfo.InvariantCulture);
      int sunriseHour = int.Parse(data.Sunrise.Substring(0, 2), CultureInfo.InvariantCulture);
      int zuhrHour = int.Parse(data.Zuhr.Substring(0, 2), CultureInfo.InvariantCulture);
      
      if(fajrHour <= currenHour && currenHour < sunriseHour)
      {
          myTextBox.Text = data.Fajr; // or to show value fajrHour.ToString()
      }
      if(sunriseHour <= currenHour && currenHour  < zuhrHour)
      {
          myTextBox.Text = data.Zuhr; //    zuhrHour.ToString()
      }
      // don't you need a third one here?
      

      【讨论】:

      • 我认为解析成 TimeSpan 比 Int32 更好,这更像是 OOP 方式,至少因为 TimeSpan 提供(封装)比较逻辑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多