Sergio Tapia
Sergio Tapia

Reputation: 41228

How can I convert a byte[] response to a valid XDocument?

<?xml version="1.0" encoding="utf-8"?>
<rsp stat="ok">
        <image_hash>cxmHM</image_hash>
        <delete_hash>NNy6VNpiAA</delete_hash>
        <original_image>http://imgur.com/cxmHM.png</original_image>
        <large_thumbnail>http://imgur.com/cxmHMl.png</large_thumbnail>
        <small_thumbnail>http://imgur.com/cxmHMl.png</small_thumbnail>
        <imgur_page>http://imgur.com/cxmHM</imgur_page>
        <delete_page>http://imgur.com/delete/NNy6VNpiAA</delete_page>
</rsp>

This is the typical response I'll receive. I've tried the following but I get an error telling me that Non White Space Characters Cannot Be Added To Content.

XDocument response = new XDocument(w.UploadValues("http://imgur.com/api/upload.xml", values));    

Upvotes: 7

Views: 10046

Answers (2)

Dennis Rosenbaum
Dennis Rosenbaum

Reputation: 379

I experienced it is better to load the bytes in a MemoryStream and feed that to the XDocument. This way you won't have to fix any white-space issues.

byte[] responseData = w.UploadValues("http://imgur.com/api/upload.xml", values);
using(var ms = new MemoryStream(responseData)) 
{
var responseXml = XDocument.Load(ms);
}

Upvotes: 5

Henk Holterman
Henk Holterman

Reputation: 273854

byte[] responseData = w.UploadValues("http://imgur.com/api/upload.xml", values);
string responseText = Encoding.ASCII.GetString(responseData);  //  ASCII assumed
XDocument respnseXml = XDocument.Parse(responseText); 

But that Error could as well come from the values.

Upvotes: 11

Related Questions