calling web service with XML output

Posted on 2011-03-06
Last Modified: 2012-08-14
I need to make 1000+ calls to the following web service and download the XML output that results from each call -

I am fairly new to web services, so please bear with me.

I wrote some C# code with which I can call the web service - see attached.

Now, I need to call the web service with a couple of parameters -,%208,%2024&rg=1,2&so=9999

I will need to loop through various values for  "cc" and "r".

The web service returns a XML file which I plan to import into Excel.

Can someone please help me with the code that will help me download the files?

namespace comtradedata
    class Program
        static void Main(string[] args)
            StringBuilder sbSource;

            // Create the web request  
            HttpWebRequest request
                = WebRequest.Create("") as HttpWebRequest;

            request.Proxy = WebProxy.GetDefaultProxy();         

            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                // Get the response stream  
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Read it into a StringBuilder  
                sbSource = new StringBuilder(reader.ReadToEnd());

                // Console application output  

Open in new window

Question by:IUAATech

Expert Comment

ID: 35053517

Accepted Solution

mayank_joshi earned 400 total points
ID: 35055064
add the following code after:-
 // Read it into a StringBuilder  
 sbSource = new StringBuilder(reader.ReadToEnd());

  Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
  Response.Charset = "";
  // If you want the option to open the Excel file without saving then
  // comment out the line below
  Response.ContentType = "application/vnd.xls"; 

Open in new window


Expert Comment

ID: 35057545
I think,
You should use XML classes available in .NET (like XMLDocument, XMLTextReader) to parse the response stream in XML and then you can convert your XML in Excel.
you should also check the HttpResponse's StatusCode property to know if the response status (i.e. check for 200)
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


Expert Comment

ID: 35057580
Forget to add example code of XmlTextReader:
using System.Xml;  
// Retrieve XML document  
XmlTextReader reader = new XmlTextReader("Your WEB-SERVICE URL");  
// Skip non-significant whitespace  
reader.WhitespaceHandling = WhitespaceHandling.Significant;  
// Read nodes one at a time  
while (reader.Read())  
    // Print out info on node  
   //do some operations.

Open in new window


Author Comment

ID: 35058520
I am trying mayank_joshi's approach.

Which namespace should I use for "Response" and HttpCacheability? I already have a reference to System.Web namespace in my code.

Assisted Solution

gery128 earned 100 total points
ID: 35065713
HttpCacheability is an enum which comes under System.Web. So you should get it if you are importing that namespace. Try full namespace name, System.Web.HttpCacheability.
Also for Response, you may be trying to use Response in demo console application you have created. Response is the property of the System.Web.UI.Page class, If you try it in ASP.NET application it will work. Response is not available in a Console Application as there is no HttpContext.
You may try WebClient class to generate request and get response in console application, but you will get response string only you won't be able to generate Excel file using System.Web.UI.Page.Response.
Check out this link:
WebClient in Console Application
Which type of application are you targeting? ASP.NET or Console Application (executable) ? If Console Application then, you need to change your approach related to Response.

Author Comment

ID: 35069415
thanks gery128 - yeah, I am targeting a console app and I got it to work. thanks.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in JQuery 5 51
Is there any online third party tool For Conducting Online Exams like Freelancer site ? 7 36
Install Problem 13 29
2 questions 10 25
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

785 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