加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使if语句根据当前时间显示xml属性

发布时间:2020-12-15 17:25:52 所属栏目:百科 来源:网络整理
导读:我有这样的 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; }} 还有
我有这样的 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的值和日出值之间,则myTextBox应显示Fajr的值.
如果当前时间的值在日出和Zuhr之间,那么显示Zuhr?

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

解决方法

首先根据@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之间:))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读