Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert Byte Array response from WebClient to Xml?

Tags:

c#

asp.net

I am calling a third party service and they send the response as Xml. However, as I am using WebClient to call the service the response I get is a byte array.

var client = new WebClient();
var result = client.UploadValues(post_url, data);

result is a byte array. How do I convert it to XML to read the response given by the third party service?

like image 356
shashi Avatar asked Oct 11 '11 09:10

shashi


2 Answers

You can turn the bytes into a string:

string xml = Encoding.UTF8.GetString(result);

and then parse it :

XDocument doc = XDocument.Parse(xml);
like image 59
Henk Holterman Avatar answered Oct 14 '22 16:10

Henk Holterman


Use a MemoryStream:

using (var stream = new MemoryStream(result))
{
    var doc = XDocument.Load(stream);
    ...
}
like image 45
Thomas Levesque Avatar answered Oct 14 '22 17:10

Thomas Levesque