Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
595
Returning Raw XML
Businesses quickly realized the potential of exchanging data in
XML format over the internet. Even before the SOAP protocol became the popular
standard for XML data exchange companies started building data exchange
applications based on XML and the HTTP protocol. A client could request data by
sending an HTTP request to a web server. The URL would identify what data the
client wanted to retrieve. The requester could even pass parameters through the
standard HTTP mechanisms either as part of the query string or in the body of
an HTTP POST request, to provide details about the data it wanted to retrieve.
A script on the server, like an ASP page, could then parse the request and
dynamically generate the XML document to return. If you wanted to share your
product data, for example, you could expose it through an ASP page and let
everybody know that they can retrieve it through an URL like this:
http://product.southrain.com/product.asp?id=100
Your business partner can write an application on their platform
of choice that will issue an HTTP request to your web server to retrieve your
product data. After the server processed the request it could respond with an
XML document:
Figure 17.6: Business can exchange XML data over HTTP with
ASP pages.
This approach does not allow for an elegant programming model
like SOAP does, but it gets the job done and gives us complete freedom over the
format of the returned XML document. It also saves the overhead associated with
the SOAP formatting each request and maintaining a WSDL document.
Writing the ASP.NET page to return an XML document is very
simple, especially when we already built database access components to return
XML. The following example shows a page calling a data access component to
retrieve XML data. This component is part of our sample application available
on our web site. Encapsulating the code to retrieve the data and generate the
XML in separate classes helps keeping the ASP.NET code in the page simple and
easy to maintain. It also packages the data access logic in a re-usable
component which you can maintain and test separately from the web page.
int.Parse( Request["id"] //
int.Parse turns a string into an int
) ) );
%>
Since we are not trying to return a web page this page does not
contain any HTML tags whatsoever. The first three lines are ASP.NET directives,
identified by the <%@. Directives are special instructions for the ASP.NET
page processor. They help setting up the page for the compilation into an
assembly. The ContentType attribute of the Page directive on the first line
indicates that this particular HTTP response contains XML and not HTML. The
attribute generates a special field defined by the HTTP protocol which clients
can read to process the body of the response correctly. Internet Explorer, for
example, displays XML as a tree with expandable nodes when the content type
indicates XML.
The second line instructs the ASP.NET processor to load the
SouthRain assembly, which contains the ProductManager class.
NOTE: You do not need to load assemblies explicitly if you
already set a reference in the Visual Studio.NET project for the web
application.
The third line declares the ProductManager class’ namespace to
save typing the namespace qualified name where we reference the ProductManger
class. You can think of the Import directive and the ASP.NET counterpart to the
“using” statement in C# classes.
The next line is the XML document declaration which is written to
the response as it is, because it is not enclosed in a <% … %> render
block. Finally we load the XML data from the ProductManager object. The
ProductManager class exposes the GetProduct() method to obtain product data in
XML format by a product’s id number. We write the result of this method
directly to the response body to return it to the requestor.
That was easy, wasn’t it? Now let’s see how we can turn XML data
into an interface for a human, not for another software application.