0

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" 
    />
Megaoctane
  • 919
  • 3
  • 9
  • 10
  • This is a duplicate question Mega, I flagged your old one for closing. In the future you should just revise your old question or delete it. – Chuck Savage May 08 '12 at 19:42

1 Answers1

3

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);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Why not a constructor that takes an XElement instead of the FromXElement? – Chuck Savage May 08 '12 at 19:35
  • @ChuckSavage: I've started finding that named static methods can be more readable than constructors, particularly when you might want two such methods with the same parameter types, e.g. `TimeSpan.FromHours` and `TimeSpan.FromDays`. For conversions I usually use static `FromXyz` methods and instance `ToXyz` methods. – Jon Skeet May 08 '12 at 19:37
  • hmm.. i got really basic skills in c#, and now i am stuck, as i am not getting anything from the table. The problem is my XML does not containg year. One Attribute for day, and one attribute for month. And it works as it is right now, but with issues regarding month end. Also it says that FromXElement is not found in class Bonn... – Megaoctane May 08 '12 at 20:06
  • @Megaoctane: If it doesn't contain the year, just use the current year, possibly adding a year if it looks like the element is for the next year (based on month/day). You'd need to write the `FromXElement` method yourself, as I suggested - that's where you'd put the code which constructs a new instance based on an `XElement`. It sounds like you may be a little out of your depth at the moment... I'd certainly start by getting this working in a small console app before putting it into WP7. – Jon Skeet May 08 '12 at 20:10