We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 89,063 times

Contents

Related Categories

Reading, Storing and Transforming XML Data in .NET - The Code

DMarko1

The Code

Here's the code for this article, and afterwards I'll go into further detail. If you wish, you may now copy and paste the code below to an .aspx file and the XSL code below to pubs.xsl for a quick glance at the results, which may make it easier to follow what going on as we progress. Incidentally, this example utilizes SQL Server Pubs database, so you'll need that ready to go. So, here goes:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat="server">
void Page_Load(){
    Get_XML_Data();
    //Get_XML_DataGrid();
}
void Get_XML_DataGrid(){
    DataSet myDataSet = new DataSet();
    myDataSet.ReadXml(Server.MapPath("pubs.xml"));
    xmlDG.DataSource = myDataSet;
    xmlDG.DataBind();
}

void Get_XML_Data() {
    string strConnect = "server=(local);uid=sa;pwd=;database=pubs;";
    SqlConnection objConnect = new SqlConnection(strConnect);
    SqlDataAdapter objCommand = new SqlDataAdapter("SELECT * FROM authors order by au_lname asc", objConnect);
    objConnect.Open();
    DataSet objDS = new DataSet();
    objCommand.Fill(objDS);
    objDS.DataSetName = "PubsList";
    if (!File.Exists(Server.MapPath("pubs.xml"))) {
        objDS.WriteXml(Server.MapPath("pubs.xml"), XmlWriteMode.IgnoreSchema);
    }
    XmlDocument XMLdoc = new XmlDocument();
    XMLdoc.Load(Server.MapPath("pubs.xml"));
    XslTransform XMLtrans = new XslTransform();
    XMLtrans.Load(Server.MapPath("pubs.xsl"));
    xmloutput.Document = XMLdoc;
    xmloutput.Transform = XMLtrans;
    objConnect.Close();
}
</script>
<ASP:DataGrid id="xmlDG" runat="server" />
<asp:xml id="xmloutput" runat="server" />

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments

  • Posted by DMarko1 on 23 Mar 2005


    Hey Kurt,

    The xsl file is the xml transformation file which parses the xml into more readable html.

  • xsl

    Posted by kurtos on 07 Dec 2004

    i might be very slow here, but what part of the code here are supposed to be used in the pubs.xsl file? yes, I am a newbie :-)

    Regards,
    Kurt
    Norway

  • Posted by James Crowley on 18 Jan 2004

    Thanks - I'll check that and edit the article to fix it :)

  • Coding comment

    Posted by resalem on 12 Nov 2003

    Very good example, simple but contains a lot of information. Only one part I had to modify, in the XSL I had to change the criteria for the select from "PubsList/Pubs" to "PubsList/Table" as the child...