Library tutorials & articles
Reading, Storing and Transforming XML Data in .NET
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" />
Related articles
Related discussion
-
handling special character in xslt's
by bussureddy82 (1 replies)
-
Reading an RSS file in ASP .NET
by TimL (31 replies)
-
Create a Site Search Engine in ASP.NET
by Soundguy53 (64 replies)
-
Cross page postbacks in ASP.NET 2.0
by PhilWhite (9 replies)
-
virtual directory asp.net
by yogeshkadvekar (3 replies)
Related podcasts
-
Writing FaceBook Applications with .NET - Interview with Mel Sampat, author of Outsync
In this episode, Scott talks with Mel Sampat, a Program Manager at Microsoft who's written OutSync, an application that syncs faces between Outlook, Facebook, and indirectly Windows SmartPhones. They chat about what it takes to write your own FaceBook application using ASP.NET or WinForms.
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
Hey Kurt,
The xsl file is the xml transformation file which parses the xml into more readable html.
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
Thanks - I'll check that and edit the article to fix it
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 node is table and not pubs. I also had to grant write access to ASPNET account for the folder where I'm creating the XML.
Thanks,
Reem
This thread is for discussions of Reading, Storing and Transforming XML Data in .NET.