Flo
Flo

Reputation: 27455

Problems transforming a REST response to a XDocument

I'm actually playing around with the last.FM web serivce API which I call via REST. When I get the response I try to convert the result into a XDocument so I could use LINQ to work with it.

But when I pass the result string to the XDocumnet constructor an ArgumentException is thrown telling me that "Non white space characters cannot be added to content.". Unfortunately I'm very new to web services and XML programming so I don't really know how to interpret this exception.

I hope someone could give me a hint how to solve this problem.

Upvotes: 2

Views: 2512

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

Here's a sample you can use to query the service:

class Program
{
    static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        using (Stream stream = client.OpenRead("http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=Cher&album=Believe"))
        using (TextReader reader = new StreamReader(stream))
        {
            XDocument xdoc = XDocument.Load(reader);
            var summaries = from element in xdoc.Descendants()
                    where element.Name == "summary"
                    select element;
            foreach (var summary in summaries)
            {
                Console.WriteLine(summary.Value);
            }
        }
    }
}

Upvotes: 3

Lusid
Lusid

Reputation: 4528

It sounds to me as though you are holding the response in a string. If that is the case, you can try to use the Parse method on XDocument which is for parsing XML out of a string.

string myResult = "<?xml blahblahblah>";
XDocument doc = XDocument.Parse(myResult);

This may or may not solve your problem. Just a suggestion that is worth a try to see if you get a different result.

Upvotes: 4

Sebastian
Sebastian

Reputation: 819

http://jamescrisp.org/2008/08/08/simple-rest-client/ has posted a little REST Client. Maybe a starting point for you.

Upvotes: 1

Related Questions