[RESOLVED] LINQ-To-XML Help Needed

Twitter API "Rate Limit Status"

Page 1 of 1

1 Replies - 1350 Views - Last Post: 21 September 2009 - 12:07 PM Rate Topic: -----

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

[RESOLVED] LINQ-To-XML Help Needed

Posted 21 September 2009 - 11:59 AM

So I am running into an issue with LINQ-To-XML with the Twitter API.

The API method "Rate Limit Status" returns Reset Time, Remaining Hits, Hourly Limit, and Reset Time In Seconds. However, the format that is returned is causing me a problem with LINQ-To-XML.

<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <remaining-hits type="integer">148</remaining-hits>
  <reset-time type="datetime">2009-09-21T18:57:16+00:00</reset-time>
  <hourly-limit type="integer">150</hourly-limit>
  <reset-time-in-seconds type="integer">1253559436</reset-time-in-seconds>
</hash>



Now the problem is that the elements that I need to read are part of the ROOT. I have no idea how to read those.

Here is the code I try:

private RateLimit GetRateLimit(string response)
{
    RateLimit rateLimit = new RateLimit();

    if (!response.Contains("ERROR"))
    {
        try
        {
            XDocument document = Xdocument.Parse(response, LoadOptions.None);

            var query = from e in document.Root.Descendants("hash")
                        select new RateLimit
                        {
                            ResetTime = e.Element("reset-time").Value,
                            RemainingHits = int.Parse(e.Element("remaining-hits").Value),
                            HourlyLimit = int.Parse(e.Element("hourly-limit").Value),
                            ResetTimeInSeconds = Int64.Parse(e.Element("reset-time-in-seconds").Value)
                        };

            rateLimit = query.First();
        }
        catch { }
    }
    else
    {
        ShowErrorMessage(response);
    }

    return rateLimit;
}



in the above code, "response" is the xml data and the RateLimit class is a custom class I have made.

Using this code returns null. It seems there are no descendants of the "hash" element.

I have no problems with the other 20+ methods I have done that also use LINQ-To-XML. But those methods return data that is better formatted.

So how do I access the elements that are part of the root? I am hoping somebody here has worked with this API method using LINQ-To-XML.

This post has been edited by eclipsed4utoo: 21 September 2009 - 12:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [RESOLVED] LINQ-To-XML Help Needed

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: [RESOLVED] LINQ-To-XML Help Needed

Posted 21 September 2009 - 12:07 PM

this happens to me everytime. I have a problem, I spend hours trying to fix it. So I come here and make a post, then 5 minutes later, I figure out my problem.

to fix my issue, I just treated the ROOT as an element...

XDocument document = Xdocument.Parse(response, LoadOptions.None);
XElement ele = document.Root;

RateLimit rate = new RateLimit();
rate.HourlyLimit = int.Parse(ele.Element("hourly-limit").Value);
...
...



God I feel so retarded sometimes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1