开发者

WP7 Return the last 7 days of data from an xml web service

I'm trying to return the last 7 days of data from an xml web service but with no luck. Could som开发者_Python百科eone please explain me to how I would accomplish this? The XML is as follows:

<node>
    <api>
        <usagelist>
            <usage day="2011-01-01">
                <traffic name="total" unit="bytes">23579797</traffic> 
            </usage>
            <usage day="2011-01-02">
                <traffic name="total" unit="bytes">23579797</traffic> 
            </usage>
            <usage day="2011-01-03">
                <traffic name="total" unit="bytes">23579797</traffic> 
            </usage>
            <usage day="2011-01-04">
                <traffic name="total" unit="bytes">23579797</traffic> 
            </usage>
        </usagelist>
    </api>
</node>

EDIT

The data I want to retrieve will be used to populate a line graph. Specificly I require the day attribute value and the traffic element value for the past 7 days. At the moment, I have the code below in place, howevewr it's only showing the first day 7 times and traffic for the first day 7 times.

XDocument xDocument = XDocument.Parse(e.Result);

        var values = from query in xDocument.Descendants("usagelist")
                   select new History
                   {
                       day = query.Element("usage").Attribute("day").Value,
                       traffic = query.Element("usage").Element("traffic").Value
                   };

        foreach (History history in values)
        {
            ObservableCollection<LineGraphItem> Data = new ObservableCollection<LineGraphItem>()
            {
                new LineGraphItem() { yyyymmdd = history.day, value = double.Parse(history.traffic) },
                new LineGraphItem() { yyyymmdd = history.day, value = double.Parse(history.traffic) },
                new LineGraphItem() { yyyymmdd = history.day, value = double.Parse(history.traffic) },
                new LineGraphItem() { yyyymmdd = history.day, value = double.Parse(history.traffic) },
                new LineGraphItem() { yyyymmdd = history.day, value = double.Parse(history.traffic) },
                new LineGraphItem() { yyyymmdd = history.day, value = double.Parse(history.traffic) },
                new LineGraphItem() { yyyymmdd = history.day, value = double.Parse(history.traffic) },
            };

            lineGraph1.DataSource = Data;
        }


This will give you a list which contains elements with Date and Traffic properties that are from the last 7 days based on your XML:

var trafficList = xmlDoc.Descendants("usage")
                        .Where(d => (DateTime.Now.Date - DateTime.Parse(d.Attribute("day").Value).Date) <= TimeSpan.FromDays(7))
                        .Select(d => new 
                                { Day = d.Attribute("day").Value, 
                                  Traffic = d.Descendants("traffic").First().Value 
                                })
                        .ToList();

Now that you have the data you can create a new collection and add the traffic data one by one:

ObservableCollection<LineGraphItem> Data = new ObservableCollection<LineGraphItem>();
foreach (var history in trafficList)
    Data.Add(new LineGraphItem() { yyyymmdd = history.Day, value = double.Parse(history.Traffic) });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜