根据日期,拿到今天和未来两天从XML值? [英] Based on date, get todays and the next two days values from XML?

查看:105
本文介绍了根据日期,拿到今天和未来两天从XML值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:$基于今天的日期和月份B $ B,获得实际的和未来两天从XML值



问题:
虽然我c.Attribute(日)。值的变化,我的c.Attribute(月)。值保持不变。所以,如果实际的一天,我30.04.2012然后它会显示30.04.2012 prayertimes但不01.05.2012 02.05.2012和。如何解决此问题?



另外,我不知道这是否 =拿督c.Attribute(日)。值+。 + c.Attribute(月)。值+。 + myDay.Year.ToString()是什么好?我想列表框来显示它是获取XML的日期。



请帮忙。
我的代码,XML及以下类。

  VAR filteredData3 =从C中loadedCustomData.Descendants(PrayerTime) 
,其中int.Parse(c.Attribute(日)值。)> = myDay.Day和放大器;&安培; int.Parse(c.Attribute(日)值。)< (myDay.Day + 3)
和;&安培; c.Attribute(月)。价值== myDay.Month.ToString()


选择新波恩()
{

拿督= c.Attribute(日)。值+。 + c.Attribute(月)。值+。 + myDay.Year.ToString(),
=晨TimeSpan.Parse(c.Attribute(黎明)。值),
日出= TimeSpan.Parse(c.Attribute(日出)。值),
Zohr = TimeSpan.Parse(c.Attribute(Zohr)。值),
晡= TimeSpan.Parse(c.Attribute(晡)。值),
=马格里布TimeSpan.Parse(c.Attribute(昏礼)值),
宵= TimeSpan.Parse(c.Attribute(宵)。值),
Jumma = TimeSpan.Parse (c.Attribute(Jumma)值),

};

listBox1.ItemsSource = filteredData3;

下面是我的类:

 公共类波恩
{

公开时间跨度晨{搞定;组; }
公开时间跨度日出{搞定;组; }
公开时间跨度Zohr {搞定;组; }
公开时间跨度晡{搞定;组; }
公开时间跨度马格里布{搞定;组; }
公开时间跨度宵{搞定;组; }
公开时间跨度Jumma {搞定;组; }

公共字符串拿督{搞定;组; }

}

下面是我的XML:

 < PrayerTime 
达格=30
鬃=4
晨=04:09
日出=05:19
Zohr =13:19
晡=18:30
马格里布=21:14
=宵22:24

/>
< PrayerTime
达格=1
鬃=5
晨=04:08
日出=05:16
Zohr =13:19
晡=18:31
马格里布=21:17
宵=22:25

/ >
< PrayerTime
达格=2
鬃=5
晨=04:06
日出=05:13
Zohr =13:19
晡=18:33
马格里布=21:19
宵=22:27
/>


解决方案

我建议你改变你的模型类使用的DateTime 而不是字符串的日期。变换的所有的元素到模型类,的然后的过滤器。这将是比试图基于属性做算术简单多了。



另外请注意,使用显式转换从 XAttribute 比调用 int.Parse 到处简单。我真的建议你的模型类创建一个静态的 FromXElement 方法,所以你可以写:

  DateTime的开始= DateTime.Today; 
//我们将以此为*独家*上限
日期时间结束= start.AddDays(3);

VAR的查询=从C中loadedCustomData.Descendants(PrayerTime)
让波恩=Bønn.FromXElement(C)
,其中bonn.Dato> =&开始放;&放; bonn.Dato<结束;
选择波恩;

或在扩展方法的语法:

  //开始和结束的
VAR查询之前= loadedCustomData.Descendants(PrayerTime)
。选择(C =>Bønn.FromXElement(C))
。凡(波恩=> bonn.Dato> =&开始放;&安培; bonn.Dato< END);


Goal: Based on todays date and month, get actual and the next two days values from XML.

Issue: Although my c.Attribute("Day").Value changes, my c.Attribute("Month").Value stays the same. So if actual day i 30.04.2012 then it will show prayertimes for 30.04.2012 but not 01.05.2012 and 02.05.2012. How to solve this?

Also i am not sure if this Dato = c.Attribute("Day").Value + "." + c.Attribute("Month").Value + "." + myDay.Year.ToString(), is any good? I want the listbox to show the date of the xml it is getting.

Please help. My code, XML and class below.

var filteredData3 = from c in loadedCustomData.Descendants("PrayerTime")
    where int.Parse(c.Attribute("Day").Value) >= myDay.Day && int.Parse(c.Attribute("Day").Value) < (myDay.Day + 3) 
    && c.Attribute("Month").Value == myDay.Month.ToString()


        select new Bønn()
        {

        Dato = c.Attribute("Day").Value + "." + c.Attribute("Month").Value + "." + myDay.Year.ToString(),
        Fajr = TimeSpan.Parse(c.Attribute("Fajr").Value),
        Sunrise = TimeSpan.Parse(c.Attribute("Sunrise").Value),
        Zohr = TimeSpan.Parse(c.Attribute("Zohr").Value),
        Asr = TimeSpan.Parse(c.Attribute("Asr").Value),
        Maghrib = TimeSpan.Parse(c.Attribute("Maghrib").Value),
        Isha = TimeSpan.Parse(c.Attribute("Isha").Value),
        Jumma = TimeSpan.Parse(c.Attribute("Jumma").Value),

        };

listBox1.ItemsSource = filteredData3;

Here is my class:

public class Bønn
{

    public TimeSpan Fajr { get; set; }
    public TimeSpan Sunrise { get; set; }
    public TimeSpan Zohr { get; set; }
    public TimeSpan Asr { get; set; }
    public TimeSpan Maghrib { get; set; }
    public TimeSpan Isha { get; set; }
    public TimeSpan Jumma { get; set; }

    public string Dato { get; set; }

}

Here is my XML:

<PrayerTime
        Dag ="30" 
        Måned="4" 
        Fajr="04:09" 
        Sunrise="05:19" 
        Zohr="13:19" 
        Asr="18:30"
        Maghrib="21:14" 
        Isha="22:24" 

    />
    <PrayerTime
        Dag ="1" 
        Måned="5" 
        Fajr="04:08" 
        Sunrise="05:16" 
        Zohr="13:19" 
        Asr="18:31"
        Maghrib="21:17" 
        Isha="22:25" 

    />
    <PrayerTime
        Dag ="2" 
        Måned="5" 
        Fajr="04:06" 
        Sunrise="05:13" 
        Zohr="13:19" 
        Asr="18:33"
        Maghrib="21:19" 
        Isha="22:27" 
    />

解决方案

I would suggest you change your "model" class to use DateTime instead of string for the date. Transform all the elements into your model class, and then filter. It'll be a lot simpler than trying to do arithmetic based on the attributes.

Also note that using the explicit conversions from XAttribute is simpler than calling int.Parse everywhere. I would actually suggest creating a static FromXElement method in your model class, so you can write:

DateTime start = DateTime.Today;
// We'll use this as an *exclusive* upper bound
DateTime end = start.AddDays(3);

var query = from c in loadedCustomData.Descendants("PrayerTime")
            let bonn = Bønn.FromXElement(c)
            where bonn.Dato >= start && bonn.Dato < end;
            select bonn;

Or in extension method syntax:

// start and end as before
var query = loadedCustomData.Descendants("PrayerTime")
                .Select(c => Bønn.FromXElement(c))
                .Where(bonn => bonn.Dato >= start && bonn.Dato < end);

这篇关于根据日期,拿到今天和未来两天从XML值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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