January 21, 2021
Hot Topics:

Managed Extensions Example That Calls the Amazon Web Service

  • By Tom Archer
  • Send Email »
  • More Articles »

Using the XmlTextReader to Invoke a Web Service

The easiest way to call a Web service is to let the .NET XmlTextReader do the heavy lifting. When you pass the URL of a Web service to the XmlTextReader constructor, it actually makes the call for you and then puts the data into an XmlTextReader object—just as if you had read an XML file from disk!

From there, you can either read through the data sequentially (the XmlTextReader is a fast, non-cached, forward-only reader for XML data) or you can load the data into an XmlDocument object where you can dynamically position it within the XML document per your application's needs—such as in searching and modifying specific node values.

  1. The first thing you'll need to do is add the following using namespace entries:
    using namespace System::Xml;
    using namespace System::Text;
  2. The following class (AmazonClient) contains a static function (FindISBN) that is all you need:
    class AmazonClient
      static void FindISBN(String* isbn)
          StringBuilder* url =  new
          webservices-20");url->Append(S"&dev-t=TOKEN");  // <=== Insert your Amazon developer token here
          url->AppendFormat(S"&AsinSearch={0}", isbn);
          Console::WriteLine(S"Searching Amazon for {0}\n", isbn);
          XmlTextReader* reader = new XmlTextReader(url->ToString());
          // display some sample data
          while (reader->Read())
            if (XmlNodeType::Element == reader->NodeType)
              if (0 == String::Compare(reader->Name, S"Authors"))
              else if (0 == String::Compare(reader->Name, S"Author"))
              else if (0 == String::Compare(reader->Name, S"Manufacturer"))
              else if (0 == String::Compare(reader->Name, S"ProductName"))
        catch(Exception* e)
  3. Finally, to call this class' lone static function, you simply pass it the desired ISBN as follows:
    Note that the code first builds a string containing the Amazon Web Service URL. It then passes that string to the XmlTextReader object, which calls the Web service, parses the returned data, and fills the XmlTextReader object. From there, it uses a simple while loop where each node read from the XmlTextReader object is checked.

    Even if you're new to XML programming, you can see that 1) a node is identified as an element by virtue of its node type being set to XmlNodeType::Element. The name of that element can then be retrieved via the Name property. If the element is one in which you wish to retrieve its value, you then call the XmlTextReader::ReadString method.

    (For those of you looking for more information on using the .NET XML class from MFC, I have an entire chapter devoted to that in my book, Extending MFC Applications with the .NET Framework.

It's Not So Hard After All

Although the example of calling a Web service that this article provided is simple and non-typed, I hope it did accomplish its main objective: illustrating how easy the .NET XmlTextReader class can make calling a Web service. Also note that while I used the Amazon Web Service as an example, what you read here will apply to virtually any Web service.

Download the Code

To download the accompanying source code for this tip, click here.

About the Author

The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.

Page 2 of 2

This article was originally published on July 28, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date