Maximus Decimus
Maximus Decimus

Reputation: 5281

StreamContent from HttpResponseMessage into XML

I'm calling an existing get method from a WebApi Controller which has this code (I cannot modify it)

   [HttpGet]
    public HttpResponseMessage Get()
    {
        XmlDataDocument xmldoc = new XmlDataDocument();
        FileStream fs = new FileStream("d:\\document.xml", FileMode.Open, FileAccess.Read);
        xmldoc.Load(fs);
        string str = xmldoc.DocumentElement.InnerXml;
        return new HttpResponseMessage() { Content = new StringContent(str, Encoding.UTF8, "application/xml") };

    }

I've been trying to read this information like this

            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.GetAsync("http://localhost/api/info");
            HttpContent content = rm.Content;

I get a StreamContent but what I like to do now is to read this content and try to deserialize it into a Xml Document in order to read the nodes.

How can I get this information from the Stream Content of the HttpContent?

Upvotes: 2

Views: 7591

Answers (1)

Dealdiane
Dealdiane

Reputation: 4064

string response;

using(var http = new HttpClient())
{
    response = await http.GetStringAsync("http://localhost/api/info");
}

var xml = new XmlDataDocument();
xml.LoadXml(response);

You can use GetStringAsync to get a string instead of an HttpContent object. You also missed the await in your GetAsync.

Note: code has not been tested

Upvotes: 1

Related Questions