Solved

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

Posted on 2012-03-16
10
1,085 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

623 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