gpt4 book ai didi

c# - 如何使 if 语句显示基于当前时间的 xml 属性

转载 作者:数据小太阳 更新时间:2023-10-29 02:11:16 26 4
gpt4 key购买 nike

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

还有一些像这样获得值(value)的东西:

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”?

最佳答案

首先按照@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 之间 :))

关于c# - 如何使 if 语句显示基于当前时间的 xml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10485767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com