in

Dave Mackey

Dave Mackey's Computers and Programming Blog.


How To: Using XML in ASP.NET.

    Everyone knows about XML, but our familiarity with actually implementing in applications may vary. I personally have had minimal needs for XML thus far. As a hobbyist programmer I was mainly using SQL databases and the only significant portion of my site using XML was the web.sitemap files that ASP.NET 2.0 provides and I utilize in Visual Studio 2005 to create navigation.

    Recently, however, I realized I needed more flexibility to arrange some links than was provided by the navigation controls in Visual Studio and at the same time need a better way than manually entering the links onto a page. This led me to the idea of using a DataList to display XML data in multiple columns. Yes, I could wire up a database to provide the data for this datalist, but I really just need a quick and easy solution...So XML is the way to go.

   Anyways, I read some of W3School's tutorials on XML. I also looked at some quickstart info. over at ASP.NET, but still wasn't entirely clear on how to implement this so I figured I'd write a quick little tutorial for anyone who might stumble upon it and need the same.

   First, let's create a new website and call it XMLSandbox. On the default.aspx page lets drop a XmlDataSource control and a DataList control. Then lets add a XML file. In the XML file lets add some sample data like so:
<famouspeople>
<famousperson>
  <name>Abraham Lincoln</name>
</famousperson>
<famousperson>
  <name>Napoleon Bonaparte</name>
</famousperson>
</famouspeople>
    Great. Now, we could have formatted this information differently, more like the web.sitemaps:
<famouspeople>
<famousperson name="Abraham Lincoln" />
<famousperson name="Napoleon Bonaparte" />
</famouspeople>
    However this is a bit messier than the first instance. If we had gone with this second instance it would have been quite easy to implement the DataList, we would have just added an ItemTemplate:
<ItemTemplate>
<%# eval("name") #>
</ItemTemplate>
    But because we chose to do it this way we need to add an XSLT file which tells the XML parser to interpret the element name as an attribute (in effect reading it as if it was the second method).
    So we add an XSLT file and enter something like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="navigation">
    <famouspeople>
      <xsl:apply-templates select="famousperson" />
    </site>
  </xsl:template>
  <xsl:template match="famousperson">
    <site>
      <xsl:attribute name="name">
        <xsl:value-of select="name" />
      </xsl:attribute>
    </site>
  </xsl:template>
</xsl:stylesheet>
   This tells the XML parser that the element "name" should instead be interpreted as the attribute "name" (staying the same simply for consistency sake).
   To make this all actually work we have to tell the XMLDataSource to look at the XML file for the data and the XSL file for the
schema (that is, method to interpret the XML file) and then tell the DataList to use the XMLDataSource as its data source. Once this is done the application should run.
   Feel free to ask me questions, but I don't know any more about XML then you do. I am largely indebted to the DataGrid or Repeater thread over at VelocityReviews for the idea of using the DataList to perform this function and to Raj Kaimal for my understanding of how to create and implement an XSL file (sorry Raj if I butchered your examples). If you have questions about XSLT files you may want to start by reading Raj Kaimal's article, "Using XSLT files with the New XMLDataSource control."

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add


Other David Mackey Sites:
Church Resources. - Christian & Family Films. - Koine Greek Open Source Audio. - BetterNeighbours.Com. -
Free Computer Wargames & Strategy Games. - W.R. Hutsell's Games. - Wandering Mind's Quotation Collection. 
- Civil War Search Directory.

Copyright David Mackey, 2006-2007. All rights reserved.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems