ASP.NET Tutorial/XML/Xml control

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

setup the xml control

   <source lang="csharp">

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

Xml Control Programming

   <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">
<xsl:for-each select="food"> <xsl:sort select="price" /> </xsl:for-each>
           
             <xsl:value-of select="name" />
           
           <xsl:value-of select="price" />
           <xsl:value-of select="description" />
           
<xsl:value-of select="calories" /> calories per serving
 </xsl:template>

</xsl:stylesheet></source>


XML transform with asp:XML

   <source lang="csharp">

<%@ 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">
       <asp:Xml ID="Xml1" 
                runat="server" 
                DocumentSource="Data.xml" 
                TransformSource="Data.xsl">
       </asp:Xml>
   </form>

</body> </html> File: Data.xml <?xml version="1.0" standalone="yes"?>

<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> <xsl:apply-templates select="Product"/>
</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></source>