How to use HttpClient to read an XML response?

mpen

Apparently HttpClient is the new recommended way of making HTTP requests, so I'm trying to use it to make a request to the Delicious API, which returns back an XML response. Here's what I've got:

internal class Program
{
    private static void Main(string[] args)
    {
        var credentials = new NetworkCredential("username", "password");
        var handler = new HttpClientHandler { Credentials = credentials};
        var client = new HttpClient(handler);

        var suggest = new Uri("https://api.del.icio.us/v1/posts/suggest");

        var suggestions =
            client.GetAsync(suggest.AddQueryParams("url", "https://yahoo.com"))
                .ContinueWith(t => t.Result.Content.ReadAsAsync<DeliciousSuggest>())
                .Unwrap()
                .Result;



        Console.ReadLine();
    }
}

public class DeliciousSuggest
{
    public string[] Popular { get; set; }
    public string[] Recommended { get; set; }
    public string[] Network { get; set; }
}

However, it throws an exception on the ReadAsAsync bit,

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

Perhaps I'm missing some assembly, but that strikes me as off. The return type is XML, not JSON, but I'm still not quite sure how this ReadAsAsync method works, or how I would even specify that.

A sample response looks like this:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<suggest>
<popular>yahoo!</popular>
<popular>yahoo</popular>
<popular>web</popular>
<popular>tools</popular>
<popular>searchengines</popular>
<recommended>yahoo!</recommended>
<recommended>yahoo</recommended>
<recommended>web</recommended>
<network>for:Bernard</network>
<network>for:britta</network>
<network>for:deusx</network>
</suggest>

How can I parse that into some usable format?

Yang You

"How can I parse that into some usable format?"

    [XmlRoot("suggest")]
public class DeliciousSuggest {
    [XmlElement("popular")]
    public string[] Popular { get; set; }

    [XmlElement("recommended")]
    public string[] Recommended { get; set; }

    [XmlElement("network")]
    public string[] Network { get; set; }
}

and use XmlSerializer to deserialize.


You should read the response back from del.icio.us as a string, and then you can deserialize it as follows:

var s = "this is the response from del"; 
var buffer = Encoding.UTF8.GetBytes(s); 
using (var stream = new MemoryStream(buffer)) { 
    var serializer = new XmlSerializer(typeof(DeliciousSuggest)); 
    var deliciousSuggest = (DeliciousSuggest)serializer.Deserialize(stream); 
    //then do whatever you want
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read a JSON response using HttpClient in vb.net

From Dev

How to read soap response xml in php

From Dev

How to Read XML Response using Java

From Dev

How to read soap response xml in php

From Dev

Ionic httpClient response use toast

From Dev

How to use XML response in react-native

From Dev

How to use curl with proxy and with japanese xml response

From Dev

How to use a RestTemplate xml file in MockClientHttpRequest/Response

From Dev

Read XML Response From Page

From Dev

Read xml response in unity Android

From Dev

Read xml response using php

From Dev

Why use the spread operator on an HttpClient response observable?

From Dev

How to read JSON response?

From Dev

How to stream response body with apache HttpClient

From Dev

How to log HttpClient requests, response including body?

From Dev

How to use HttpClient to Post with Authentication

From Java

How to use reportProgress in HttpClient in Angular?

From Dev

How to use Robot framework for Rest Automation with XML Body and XML Response Body

From Dev

How to read 2nd element of list of xml items returned in response using ExtractVariables policy in apigee?

From Dev

How to read 2nd element of list of xml items returned in response using ExtractVariables policy in apigee?

From Dev

How to read the response from a NewSingleHostReverseProxy

From Dev

How to read response headers in angularjs?

From Dev

How to read AJAX response variable?

From Dev

How to read response headers with $resource?

From Dev

How to read a JSON response in Unity?

From Dev

How to read webapi responses with HttpClient in C#

From Dev

How to use this JSON response?

From Dev

how to read complex XML

From Dev

how to read complex XML

Related Related

HotTag

Archive