How to read xml returned by a WebService in a IE?

This how to shows how you can read the XML which is returned by a WebService and displayed in raw XML in IE.
fozylet submitted the solution for that question in the asp.net forums. I have converted his VB.NET code to C#.

Used Classes:

StreamReader
XmlDocument
HttpWebRequest
HttpWebResponse

Code:

void Page_Load(object sender, System.EventArgs e)
{
  if(!Page.IsPostBack)
  {
    StreamReader StreamHandler;
    XmlDocument xmlDoc = new XmlDocument();

    string strURL = "
http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=USD";

    HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(strURL);
    wRequest.Headers.Add("Man", "GET " + strURL);
    HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse();
    if(wRequest.HaveResponse){
      if(wResponse.StatusCode == HttpStatusCode.OK){
        StreamHandler = new System.IO.StreamReader(wResponse.GetResponseStream());
        xmlDoc.LoadXml(StreamHandler.ReadToEnd());
        Response.Write(xmlDoc.InnerText);
      }
    }
  }
}
</script>

Output:

0.0218

Filed under:

Comments

# Christian Weyer said:

This is not a SOAP-based Web service, as you might already know. This is plain HTTP GET.
Why don't you use the cleint-side ASMX features to 'seamlessly' call the Service via SOAP and the proxy classes? With your approach you have the raw XML - and then?

Just curious ... because there is a solution to get down to the raw XML with the common ASMX-based approach.

Saturday, July 17, 2004 6:36 AM
# Sonu Kapoor said:

Christian, do you have any example for your approach. Honestly said I am new in the world of WebServices and SOAP (or Shampoo (Joking)). I would love to see how it works. Thanks anway for the information. I will try to find something about it.

Saturday, July 17, 2004 6:50 AM
# rob said:

Sonu,

Response.Write(xmlDoc.InnerText) will write on your page the whole XML document.

In this case, it's alright because the XML is very simple.

But what if the XML had more than one element and you only wanted one particular element? In other words, how do you select only parts of that xml you are getting and not the whole thing?

Saturday, July 17, 2004 1:53 PM
# Sonu Kapoor said:

Rob, the xml is stored in the xmlDoc object. That object can be used with the function SelectSingleNode to retrieve one node or SelectNodes to retrieve a list of specific nodes. Hope that helps!

Saturday, July 17, 2004 4:17 PM
# rob said:

Could you be a little more specific?

For instance, having this code in the xmldoc object, how would you get the text between the two Symbol tags?:

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.webserviceX.NET/">
<StockQuotes>
<Stock>
<Symbol>MSFT</Symbol>
<Last>28.27</Last>
<Date>7/14/2004</Date>
<Time>12:12pm</Time>
<Change>+0.67</Change>
<Open>27.40</Open>
<High>28.36</High>
<Low>27.34</Low>
<Volume>46111616</Volume>
<MktCap>305.2B</MktCap>
<PreviousClose>27.60</PreviousClose>
<PercentageChange>+2.43%</PercentageChange>
<AnnRange>24.01 - 30.00</AnnRange>
<Earns>0.87</Earns>
<P-E>31.72</P-E>
<Name>MICROSOFT CP</Name>
</Stock>
</StockQuotes>
</string>

Thanks

Monday, July 19, 2004 8:55 AM
# Sonu Kapoor said:

The MSDN contains an example for that:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlNodeClassSelectSingleNodeTopic.asp

Monday, July 19, 2004 11:37 AM
# Sonu Kapoor said:

For anybody interested else, see also:
http://asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=639424

Monday, July 19, 2004 12:00 PM
# dragonimp said:

You can call remote procedure via webservice but how can you do that with xmlobject? It costs you too much. (.Text could be an example)
I think xmldom can be used in simple situation without webservice supporting.

Monday, July 26, 2004 5:13 AM
# Adah said:

Badly need your help. If you would thoroughly know anything, teach it to others. Help me! Looking for sites on: Share trading permission. I found only this - <a href="leadership.nlada.org/.../ShareTrading">clients of share trading</a>. Share trading, if the improvement of sending with share manuscript of value is made off the handset, many transactions are intensify to set the part with disadvantage, then issuing a level of better markets for provision stock. Share trading, tonnes, the act takes usually about in the double-sided share of the stock and the results are seen to a mitigate pay-per-click. Waiting for a reply :confused:, Adah from San.

Wednesday, March 24, 2010 4:42 AM
# oem software said:

Ayrs7F I really liked your blog article.Much thanks again. Will read on...

Wednesday, May 02, 2012 4:07 PM
# bookmarking service said:

sCpJew Enjoyed every bit of your article post.Really looking forward to read more. Want more.

Thursday, September 20, 2012 12:47 PM
# icons pack said:

<a href="www.techadvice.eu/.../"> What curious topic</a>

Sunday, October 07, 2012 6:06 PM
# icons download said:

<a href="forum.edusite.ru/.../index.php It is remarkable, this amusing message</a>

Tuesday, October 09, 2012 2:02 PM
# cheap bookmarking service said:

yDDV3N Say, you got a nice blog article.Thanks Again. Fantastic.

Thursday, October 18, 2012 5:24 PM
# icons archive said:

<a href=the24hourclub.com/index.php It is very a pity to me, I can help nothing to you. I think, you will find the correct decision.</a>

Friday, November 02, 2012 4:39 PM
# Lovett said:

Thanks for this post, I am a big fan of this website want to go on updated.

Thursday, January 10, 2013 10:03 AM
# Arsenault said:

it's always a fine idea to search out event planners when you want a fine outcome for your planned event“

Monday, January 28, 2013 9:48 AM
# Spaulding said:

Thanks for any other magnificent article. Where else may

just anybody learn that kind of info in such a perfect tactic of writing?

I have a presentation next week, and I’m on that the look for such info.

Tuesday, March 05, 2013 1:48 AM
# Robles said:

Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.

You clearly know what youre talking about, why waste your intelligence on just posting videos to your site when you could

be giving us something enlightening to read?

Wednesday, April 17, 2013 4:38 PM

Leave a Comment

(required) 
(required) 
(optional)
(required)