Consuming XML in ASP.NET 2.0+


The question came into the Microsoft Expression Web forum. I would have answered it there, but two things happened:

  1. Microsoft is dinking with the site tonight and it is broken as soon as you sign in with passport.
  2. I felt this was better here

Let’s ignore #1 for now, as it is out of my control and focus on #2. While this post was in Expression, it is a problem that everyone who attempts to consume XML will run into. Here is the question, so you have the context:

I have an Access 2007 Database and I want to export data in an XML format for inclusion into my web site.  While Access will export the file, it is in a format EW does not accept when I try to create a Grid View.

<?xml version="1.0" encoding="UTF-8" ?>
– <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2008-06-18T11:20:50">
– <WebMachines>
<MachineID>6407</MachineID>
<TypeMachine>AUTO SCREW MACHINES, SWISS, CNC-MILL & SUB SPDL</TypeMachine>
<Year>2000</Year>
<Manufacturer>Nomura</Manufacturer>
<Model>NN-13TB</Model>
<Control>Mitsubishi Meldas 520L</Control>
<Condition>Very Good</Condition>
<List>2000 Nomura NN-13TB Mitsubishi Meldas 520L</List>
<Code>A8175MS</Code>
<Featured>0</Featured>
</WebMachines>

Now the XML data that is in the Sample file Members.xml works fine.  I guess I am confused why EW will not accept an XML file from an up-to-date version on Access 2007???

<members>
<member isactive="true" groupcolor="#FFCC3333" name="Tom Jackson" datejoined="10/06/2006" />
<member isactive="false" groupcolor="#FFA4AB28" name="Stacey Footheart" datejoined="11/10/2006" />
<member isactive="true" groupcolor="#FFA6D877" name="Jack Herdin" datejoined="6/10/2006" />
<member isactive="true" groupcolor="#FF138C48" name="Kim Getruds" datejoined="4/15/2006" />
<member isactive="false" groupcolor="#FF5FADB9" name="Larry Nusom" datejoined="7/12/2006" />
<member isactive="true" groupcolor="#FF3B1076" name="Gary Heart" datejoined="9/21/2006" />
</members>

Is there anyway to export data from a Access 2007 database that EW will accept cleanly?  I have tried every possible way to export an XML file format that EW will accept. I thoughts these programs were suppose to work with each other cleanly?

I will show two ways to skin this cat. Before doing that, we need to clean up the XML a bit. First, all ampersands need to be convered to &amp;. The same is true if there is any other reserved word. The second is to close out the </dataroot>. And the third is to get rid of the hyphens (I assume it was copied out of Internet Exploder.

<?xml version="1.0" encoding="UTF-8" ?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2008-06-18T11:20:50">
<WebMachines>
<MachineID>6407</MachineID>
<TypeMachine>AUTO SCREW MACHINES, SWISS, CNC-MILL &amp; SUB SPDL</TypeMachine>
<Year>2000</Year>
<Manufacturer>Nomura</Manufacturer>
<Model>NN-13TB</Model>
<Control>Mitsubishi Meldas 520L</Control>
<Condition>Very Good</Condition>
<List>2000 Nomura NN-13TB Mitsubishi Meldas 520L</List>
<Code>A8175MS</Code>
<Featured>0</Featured>
</WebMachines>
</dataroot>

<asp:xml>

Our first method of solving this is the ASP.NET XML control. Since the person was attempting to do this in Expression, let’s start there.

  1. Add the XML file to the website (App_Data is the best location, IMO)
  2. Drag the XML node <WebMachines> onto the page
  3. Preview

If you have a single WebMachines tag, you end up with a vertical display of the column nodes. If you have more than one, you end up with a grid. Fairly simple stuff. The XSL does all of the dirty work and you can edit the XSL file created or use the presets.

GridView and XmlDataSource

This is a bit more involved, but still fairly simple. As the XML is not using attributes (the default for auto generated columns), you will have to tell how to render the GridView.

First, drag an XmlDataSource control on the page. Browse for the xml file and select it for the first text box. Do not fill the XSLT textbox (2nd down), but do set the XPATH to /dataroot/WebMachines.

Then, drag a GridView on the page and set it to the XmlDataSource. You then will have to add templated columns and drag a label into each one. Here is the first column (MachineID):

<asp:TemplateField HeaderText="ID">
  <ItemTemplate>
    <asp:Label runat="server" ID="lblMachineID" Text='<%#XPath("MachineID")%>’/>
</ItemTemplate>
</asp:TemplateField>

The XPATH is the key here when you are not using attributes. The complete source of the page is as follows, although I am only showing a few of the "columns" in the XML file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Page Language="C#" %>

<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:xmldatasource id="XmlDataSource1" runat="server" datafile="Test.xml" xpath="/dataroot/WebMachines">
    </asp:xmldatasource>
        <br />
        <asp:gridview id="GridView1" runat="server" datasourceid="XmlDataSource1" autogeneratecolumns="false">
          <Columns>
         <asp:TemplateField HeaderText="ID">
           <ItemTemplate>
             <asp:Label runat="server" ID="lblMachineID" Text='<%#XPath("MachineID")%>’/>
         </ItemTemplate>
         </asp:TemplateField>         
         <asp:TemplateField HeaderText="Machine ID">
           <ItemTemplate>
             <asp:Label runat="server" ID="lblTypeMachine" Text='<%#XPath("TypeMachine")%>’/>
         </ItemTemplate>
         </asp:TemplateField>
                   <asp:TemplateField HeaderText="Year">
           <ItemTemplate>
             <asp:Label runat="server" ID="lblYear" Text='<%#XPath("Year")%>’/>
         </ItemTemplate>
         </asp:TemplateField>
                 <asp:TemplateField HeaderText="Manufacturer">
           <ItemTemplate>
             <asp:Label runat="server" ID="lblManufacturer" Text='<%#XPath("Manufacturer")%>’/>
         </ItemTemplate>
         </asp:TemplateField>
                           <asp:TemplateField HeaderText="Model">
           <ItemTemplate>
             <asp:Label runat="server" ID="lblModel" Text='<%#XPath("Model")%>’/>
         </ItemTemplate>
         </asp:TemplateField>
        </Columns>    
    </asp:gridview>
    </form>
</body>
</html>

Wasn’t that quite easy? Of course, the tools do not do all of this for you, which may be "problematic" for some, but the syntax is still completely declarative.

Peace and Grace,
Greg

Advertisements

One Response to Consuming XML in ASP.NET 2.0+

  1. Pingback: Gridget

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: