如何使if语句显示基于当前时间的xml属性 [英] How to make an if statement to show xml attribute based on current time

查看:300
本文介绍了如何使if语句显示基于当前时间的xml属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的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; }
}

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

And something to get the value like this:

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的值. 如果当前时间的值在日出和祖尔之间,请显示祖尔?

How can i based by current time of day say that if time is between the value of Fajr and the Value of Sunrise, then myTextBox should show the value of Fajr. If value of current time is between sunrise and Zuhr, show Zuhr?

如何获取它以在myTextBox2中显示属性名称? 例如,myTextBox显示值"07:00",而myTextBox2显示值"Fajr"?

How can i get it to show the attribute name in myTextBox2? For example, myTextBox shows value "07:00", and myTextBox2 shows "Fajr"?

推荐答案

首先按照@abatischcev

First modify the class as per @abatischcev

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

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

Then modify the linq query select part as:

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之间:))

这篇关于如何使if语句显示基于当前时间的xml属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆