ASP.NET Tutorial/XML/Xml control
setup the xml control
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="XmlProgramming" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Xml Control Programming</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Xml Control Programming</h3>
<asp:Xml ID="myXml" runat="server">
</asp:Xml>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public partial class XmlProgramming : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string xmlUrl = Server.MapPath("Data.xml");
XPathDocument xpdoc = new XPathDocument(xmlUrl);
XPathNavigator xnav = xpdoc.CreateNavigator();
//
myXml.XPathNavigator = xnav;
myXml.TransformSource = "Data.xslt";
}
}
File: ~/Data.xml
<?xml version="1.0" encoding="utf-8" ?>
<menu>
<food>
<name>Name 1</name>
<price>$2.5</price>
<description>description 1</description>
<calories>1200</calories>
</food>
<food>
<name>Name 2</name>
<price>$35.95</price>
<description>description 2</description>
<calories>1150</calories>
</food>
<food>
<name>Name 3</name>
<price>$19.95</price>
<description>description 3</description>
<calories>1000</calories>
</food>
</menu>
File: ~/Data.xsl
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/menu">
<table width="400">
<xsl:for-each select="food">
<xsl:sort select="price" />
<tr bgcolor="goldenrod">
<td>
<b>
<xsl:value-of select="name" />
</b>
</td>
<td align="right">
<xsl:value-of select="price" />
</td>
</tr>
<tr bgcolor="palegoldenrod">
<td colspan="2">
<xsl:value-of select="description" />
<br />
<i><xsl:value-of select="calories" /> calories per serving</i>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
XML transform with asp:XML
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Xml ID="Xml1"
runat="server"
DocumentSource="Data.xml"
TransformSource="Data.xsl">
</asp:Xml>
</div>
</form>
</body>
</html>
File: Data.xml
<?xml version="1.0" standalone="yes"?>
<!--Created with the XmlDocument class.-->
<SuperProProductList >
<Product ID="1" Name="Chair">
<Price>49.33</Price>
</Product>
<Product ID="2" Name="Car">
<Price>43.55</Price>
</Product>
<Product ID="3" Name="Fresh Fruit Basket">
<Price>49.99</Price>
</Product>
</SuperProProductList>
File: Data.xsl
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" >
<xsl:template match="SuperProProductList">
<html><body><table border="1">
<xsl:apply-templates select="Product"/>
</table></body></html>
</xsl:template>
<xsl:template match="Product">
<tr>
<td><xsl:value-of select="@ID"/></td>
<td><xsl:value-of select="@Name"/></td>
<td><xsl:value-of select="Price"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>