This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
.NET and XML
Populating a TreeView Control
Handling Events
Data with a TreeView
ASP.NET XML Architecture
Code to create entire file system as XML File in VB.NET
Class object for Base64 conversion with .NET
XML Web Services and ASP.NET
String Handling Application using ASP.NET
XML Serialization and .Net
Dynamic Method Invocation in .NET using Reflection API
Using XML classes in .Net
Pass node sets to XSLT stylesheets using .NET
Output a directory as XML using .NET
.Net, COM Interoperability and Word Object Model
WS-Routing for .NET endpoints
Using XML and XSLT in VB.NET Jeopardy
Symmetric Encryption/Decryption with .NET
XInclude.NET 1.0
EXSLT.NET 1.0
How to read a XML file using XmlTextReader?
<< XSLT
 

By :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 :4314

 

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:


[Md1] 

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.

Now let’s look at the code for the page:

<%@ Page language="c#" ContentType="text/xml" %>

<%@ Assembly name="SouthRain" %>

<%@ Import namespace="CompleteXml.SouthWind.Managers" %>

<?xml version="1.0" encoding="utf-8"?>

<%

   ProductManager pm = new ProductManager();

   Response.Write(              // Write directly to the output.

     pm.GetProduct(             // GetProduct returns XML

       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.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP