HttpWebResponse.GetResponseStream converts &lt; instead of < etc.

I am using an HttpWebRequest to retrieve XML data from a web service.

My content type is:
request.ContentType = "text/xml; charset=utf-8";

The problem is, when I retrieve the text, everything inside the Envelope, the response, is converted. So <> are &lt; and &gt;, etc..

How do I retrieve the data, and retain the less than / greater than symbols for valid XML?

Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

public string XMLDecode(string Value)
    return Value.Replace ("&amp;", "&").Replace("&apos;", "'").Replace("&quot;", "\"").Replace ("&lt;", "<").Replace ("&gt;", ">");
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Well, yeah. But I thought there should be a way of retrieving the data WITHOUT it converting in the first place.

Is that not possible? Or do you not know?

since < and > are already in use by HTML so whenever the service that you are using is returning the xml response in http, it is encoding these characters and returning back.

However if the service that you are calling is a RESTFUL Service it will not return any html response, so you will get your xml already decoded.

So a lot depends a lot on what kind of web service you are working with, is it asmx or wcf service. Is this service in your control???
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Rose BabuSenior Team ManagerCommented:

use the below code to get the correct XML response.

after getting response from the request, use the streamreader to read the xml values.
this will give you the exact xml data.
string httpResponse = "";
string requestUrl = "";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);

WebResponse response = null;
StreamReader reader = null;
	response = request.GetResponse();
catch (WebException ex)
	response = ex.Response;

reader = new StreamReader(response.GetResponseStream());
httpResponse = reader.ReadToEnd();

TextBox2.Text = httpResponse;

Open in new window

Try this.

Hope this will work for you.
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:

That produces the same thing.. the < >'s are converted:

Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Decoding is not helping, because it also decodes values within the elements. Then I get an error:

>>Data at the root level is invalid. Line 1, position 1

Because the text was replaced with an open carat:

any open carets and other XML keys have to be sent as <CData></CData>
Rose BabuSenior Team ManagerCommented:
strange... but, my sample code produces XML structure correctly.

update requestUrl  with the below sample url and test once.
string requestUrl = "";

//string requestUrl = "";

Open in new window

Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
I found the solution.

I had to use HtmlDecode to decode the commands without decoding the element text.

Thanks all!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
There is only the one solution. Use: HtmlDecode
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.