setup the xml control : Xml control « XML « ASP.NET Tutorial






<%@ 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>








25.7.Xml control
25.7.1.XML transform with asp:XML
25.7.2.setup the xml control