Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-16
10
Medium Priority
?
1,126 Views
Last Modified: 2012-03-25
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?

thanks!
0
Comment
Question by:Starr Duskk
  • 5
  • 3
  • 2
10 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37731003
public string XMLDecode(string Value)
{
    return Value.Replace ("&amp;", "&").Replace("&apos;", "'").Replace("&quot;", "\"").Replace ("&lt;", "<").Replace ("&gt;", ">");
}
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 37731049
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?

thanks.
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 1000 total points
ID: 37732097
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???
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Assisted Solution

by:Rose Babu
Rose Babu earned 1000 total points
ID: 37732328
Hi,

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 = "http://www.domain.com/Service.asmx/Method1?Param1=string&Param2=string&Param3=string";

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

WebResponse response = null;
StreamReader reader = null;
try
{
	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.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 37738412
srosebabu,

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

    &lt;/client&gt;
    &lt;order_dataset&gt;
        &lt;subjects&gt;
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 37739914
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:

<class>GVWR<26001</class>
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37740786
any open carets and other XML keys have to be sent as <CData></CData>
0
 
LVL 16

Expert Comment

by:Rose Babu
ID: 37740959
strange... but, my sample code produces XML structure correctly.

update requestUrl  with the below sample url and test once.
string requestUrl = "http://constantine.lirmm.fr:8080/CentileParSaisieWS_GFV2/CentileWSService?wsdl";

//string requestUrl = "http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetWeatherInformation?";

Open in new window

0
 
LVL 2

Accepted Solution

by:
Starr Duskk earned 0 total points
ID: 37742881
I found the solution.

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

Thanks all!
0
 
LVL 2

Author Closing Comment

by:Starr Duskk
ID: 37762383
There is only the one solution. Use: HtmlDecode
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Loops Section Overview
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question