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

Sergio Tapia picture Sergio Tapia · Aug 10, 2010 · Viewed 7.9k times · Source
<?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));    

Answer

Henk Holterman picture Henk Holterman · Aug 10, 2010
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.