Solved

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

Posted on 2012-03-16
10
1,046 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 1

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
 
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: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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

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 1

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 1

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

20 Experts available now in Live!

Get 1:1 Help Now