How to convert byte array response from webclient to xml?

I call a third-party service and they send the response as Xml. However, since I use WebClient to call the service, I get an array of bytes.

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

result is an array of bytes. How to convert it to XML to read the response provided by a third-party service?

+6
source share
2 answers

Use a MemoryStream :

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

You can turn bytes into a string:

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

and then analyze it:

 XDocument doc = XDocument.Parse(xml); 
+8
source

Source: https://habr.com/ru/post/899041/


All Articles