简体   繁体   中英

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

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?

You can turn the bytes into a string:

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

and then parse it :

XDocument doc = XDocument.Parse(xml);

Use a MemoryStream :

using (var stream = new MemoryStream(result))
{
    var doc = XDocument.Load(stream);
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM