Solved

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

Posted on 2012-03-16
10
1,036 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:BobCSD
  • 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 1

Author Comment

by:BobCSD
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 250 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
 
LVL 16

Assisted Solution

by:Rose Babu
Rose Babu earned 250 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 1

Author Comment

by:BobCSD
ID: 37738412
srosebabu,

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

    &lt;/client&gt;
    &lt;order_dataset&gt;
        &lt;subjects&gt;
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:BobCSD
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 1

Accepted Solution

by:
BobCSD 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 1

Author Closing Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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.…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now