When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Wednesday, June 06, 2001

Creating a Function to Stream a Recordset to XML
By Scott Mitchell


For More Information on XML...
This article uses the Microsoft XMLDOM object to create an XML document from a Recordset. If you are not familiar with XML, or need to brush up on your XML skills, be sure to check out the many fine article in the XML Article Index. Also, if you are relatively new to XML, XML101.com is a great place to get started learning!

- continued -

AN UPDATE IS AVAILABLE
While the code in this article will work fine, David O'Neill has written an article that illustrates how to tweak the function examined in this article for better performance. Once you've read this article and understand the concepts, take a moment to check out Increasing ConvertRStoXML() Performance!

ANOTHER UPDATE!
Carlos Baptiste has written an article titled Revisiting Converting a Recordset to XML that looks at using the MSXML DOMDocument object and XSL stylesheets to transform the Recordset's native XML output into a human-readable XML output with just a few lines of code.

Introduction:
Did you know that the ADO Recordset contains a Save method that can save the contents of a Recordset to an XML file? In fact, in an upcoming article on 4Guys this method will be discussed. One disadvantage of using this approach, though, is that the XML produced by the Recordset is anything but readable. Plus, there is a ton of information there, describing gobs of data for each column in the Recordset (its data type, precision, NULLability, maxlength, etc.). Additionally, when saving a Recordset to XML, the XML node names are archaic: each row's node name is z:row; the parent node is named rs:data.

In this article we are going to examine how to create a custom function that will take the contents of a Recordset and return a much friendly-on-the-eyes XML version. Once the ASP page calls this function and has, as a string, the XML structure, it can either save it to disk or write out the contents via Response.Write. The latter approach would be a useful way for sharing a site's data with other Web sites. For example, in the live demo for this article, we will look at sharing the 10 most popular FAQs from ASPFAQs.com as XML data. In a future article we will examine various ways for another Web site to consume and display this information.

Calling the Function
Ideally, our function to convert a Recordset into a human-friendly XML formatted file would only require one parameter, the Recordset object to convert, and return a string containing the XML. However, the custom function I created expects three parameters: objRS, strTopLevelNodeName, and strRowNodeName:

xmlString = ConvertRStoXML(objRS, strTopLevelNodeName, strRowNodeName)

The strTopLevelNodeName parameter specifies the name to be used for the root node element, while the strRowNodeName parameter specifies the name for each row-level node. If this sounds a little confusing now, don't worry, an example will help clear things up. Imagine that we had a Recordset containing product information. This Recordset might have the following contents:

ProductIDProductName
1Monitor
2Mouse
4Keyboard
6Scanner
. . .

When calling the ConvertRStoXML and passing in this Recordset, the XML we'll get back will look like:

<strTopLevelNodeName>
  <strRowNodeName>
    <field name="ProductID">
      <value>1</value>
    </field>
    <field name="ProductName">
      <value>Monitor</value>
    </field>
  </strRowNodeName>
  <strRowNodeName>
    <field name="ProductID">
      <value>2</value>
    </field>
    <field name="ProductName">
      <value>Mouse</value>
    </field>
  </strRowNodeName>
  ...
</strTopLevelNodeName>

Note that for each row in the Recordset there will be a strRowNodeName node. (Hopefully it is clear, now, that the string values you pass in to the ConvertRStoXML function determine the name of the top-level node (strTopLevelNodeName) and the node name for each row (strRowNodeName). For the above example I would recommend a strTopLevelNodeName of products and a strRowNodeName of product, which would give the XML:

<products>
  <product>
    <field name="ProductID">
      <value>1</value>
    </field>
    <field name="ProductName">
      <value>Monitor</value>
    </field>
  </product>
  <product>
    <field name="ProductID">
      <value>2</value>
    </field>
    <field name="ProductName">
      <value>Mouse</value>
    </field>
  </product>
  ...
</products>

Now that we've looked at how to call the ConvertRStoXML function, we need to examine the code for the function. We'll tackle this in Part 2 of this article.

  • Read Part 2!


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES