SUMMARY
This article demonstrates how to use an XML data-island and
the .NET
Xml Web server control to create a small, multilanguage Web
Application project that displays Extensible Markup Language (XML) data. The
main application is built in Visual Basic .NET or Visual Basic 2005. The application uses a Visual
C# Class Library component to illustrate multilanguage references and
implementation inheritance.
back to the top
Requirements
The following list outlines the recommended hardware, software,
network infrastructure, and service packs that you need:
- Microsoft Windows 2000 Professional, Microsoft Windows 2000
Server, Microsoft Windows XP Professional, or Microsoft Windows XP Server with
the Microsoft .NET Framework and Microsoft Visual Studio .NET
installed
This article assumes that you are familiar with the following
topics:
- Implementation inheritance
- XML and Extensible Stylesheet Language Transformation
(XSLT)
- Microsoft .NET Web server controls
back to the top
Create a Multilanguage ASP.NET Web Application
In the following steps, you create a small ASP.NET Web
Application project that allows a customer to select lunch menu items based on
calorie count. To do this, the sample uses the
Xml Web server control, a simple XML document that contains the menu
data, and an XSL stylesheet to transform the XML data. The
XsltArgumentList class from the
System.Xml.Xsl namespace allows you to use parameters in the transformation.
- Create a Visual Basic Windows Application project as
follows:
- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click New Project.
- Under Project Types, click Visual Basic Projects. Under Templates, click ASP.NET Web Application. Rename the project Menu, and then click OK.
Note In Visual Studio 2005, click Visual Basic under Project Types.
- Make sure that the Solution Explorer window is visible. If
it is not, press the CTRL+ALT+L key combination.
- You now have the shell of a Visual Basic .NET or Visual Basic 2005 Web
Application project within a solution named Menu. To make this a multilanguage
solution, you must add a Visual C# Class Library.
In the Solution
Explorer window, right-click Menu, point to Add, and then click New Project. Under Project Types, click Visual C# Projects. Under Templates, click Class Library. Keep the default name, ClassLibrary1. Click OK to add this Class Library to the solution.
Note In Visual Studio 2005, click Visual C# under Project Types. - Class1.cs appears in the Editor window. Add the following
namespace declaration to the top of this window:
using System.Xml.Xsl;
- Under public Class1, add the following code:
public XsltArgumentList CreateArgumentList(
string ParamName, string NamespaceURI, string ParamValue)
{
XsltArgumentList tal = new XsltArgumentList();
tal.AddParam(ParamName, NamespaceURI, ParamValue);
return tal;
}
- You now have a Visual C# class that takes XSLT parameter
information and that returns an instance of the XsltArgumentList class. You can now build the Visual Basic application that will
consume this component.
WebForm1.aspx should already be open in
Design View. If not, double-click WebForm1.aspx in the Solution Explorer window. Click the Design tab below the Editor window to switch to Design View. - Press the F4 key to access the property page. Change the pageLayout property to FlowLayout.
- On the Web Form, type Maximum Calories:
.
- Add a TextBox control, a Button control, and an Xml control to the form as follows:
- Press the CTRL+ALT+X key combination to open the
toolbox.
- In the toolbox, click Web Forms.
- Drag a TextBox control and a Button control from the toolbox, and drop these controls onto the Web
Form.
- Press F4 to access the property page for the Button control.
- Change the Text property to Filter Menu.
- Position the cursor after the button, and then press
ENTER to position the cursor for the Xml control.
- Drag an Xml Web server control from the toolbox, and drop this control onto
the Web Form.
- Press F4 to access the property page of the Xml control.
- Change the DocumentSource property to Menu.xml, and then change the TransformSource property to Menu.xslt.
- Double-click Filter Menu to create a Click event handler in the code-behind class of the Web Form. Before
you add code to the handler, you must add a reference to the Visual C# Class
Library as follows:
- In the Solution Explorer window, under ClassLibrary1, right-click References, and then click Add Reference.
- On the Projects tab, double-click ClassLibrary1. Notice that ClassLibrary1 appears in the Selected Components list box.
- Click OK. Notice that ClassLibrary1 appears under References in the Web Application.
- Add the following code to the Click event handler:
Dim myArgList As New ClassLibrary1.Class1()
Xml1.TransformArgumentList = myArgList.CreateArgumentList("calories", "", TextBox1.Text)
Xml1.Visible = True
The argument list that the Visual C# component returns allows you to
pass the value of the TextBox control as a parameter to the stylesheet. This stylesheet uses
XSLT commands to filter out all lunch menu items that exceed the calorie count
that the customer types. The following line of code from the stylesheet
illustrates how the parameter is received and used:
<xsl:for-each select="lunch-menu/food[calories <= $calories]">
- Use the code from the Menu.xml and the Menu.xslt sections to create the Menu.xml and Menu.xslt
files. In the Solution Explorer window, right-click the Menu project, and then
add the .xml and .xslt files.
back to the top
Complete Code Listing
Menu.xml
<?xml version='1.0'?>
<lunch-menu>
<food>
<name>Cheese Pizza</name>
<price>$6.95</price>
<description>Individual deep-dish pizza with lots of mozzarella cheese</description>
<calories>800</calories>
</food>
<food>
<name>Pepperoni Pizza</name>
<price>$7.95</price>
<description>Individual deep-dish cheese pizza with thick-cut pepperoni slices</description>
<calories>950</calories>
</food>
<food>
<name>The "Everything" Pizza</name>
<price>$9.95</price>
<description>Individual deep-dish pizza with all our toppings. House specialty!</description>
<calories>800</calories> </food>
<food>
<name>Hungarian Ghoulash</name>
<price>$4.50</price>
<description>Large serving in a sourdough bread bowl. A_local delight!</description>
<calories>600</calories>
</food>
<food>
<name>Maisey's Pork Sandwich</name>
<price>$6.95</price>
<description>A fresh pork fillet, deep-fried to perfection. Served with fries.</description>
<calories>950</calories>
</food>
</lunch-menu>
back to the top
Menu.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="calories">1500</xsl:param>
<xsl:template match="/">
<HTML>
<BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt;
background-color:#EEEEEE">
<xsl:for-each select="lunch-menu/food[calories <= $calories]">
<DIV STYLE="background-color:blue; color:white; padding:4px">
<SPAN STYLE="font-weight:bold; color:white"><xsl:value-of select="name"/></SPAN>
- <xsl:value-of select="price"/>
</DIV>
<DIV STYLE="margin-left:20px; margin-bottom:1em; font-size:10pt">
<xsl:value-of select="description"/>
<SPAN STYLE="font-style:italic">
(<xsl:value-of select="calories"/> calories per serving)
</SPAN>
</DIV>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
back to the top
WebForm1.aspx
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="menu.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body >
<form id="Form1" method="post" runat="server">
<P>Maximum Calories:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Filter menu"></asp:Button></P>
<P>
<asp:Xml id="Xml1" runat="server" DocumentSource="menu.xml" TransformSource="menu.xslt"></asp:Xml></P>
</form>
</body>
</HTML>
back to the top
Class1.cs Non-Inheritance Example
using System;
using System.Xml.Xsl;
namespace ClassLibrary1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public XsltArgumentList CreateArgumentList(
string ParamName, string NamespaceURI, string ParamValue)
{
XsltArgumentList tal = new XsltArgumentList();
tal.AddParam(ParamName, NamespaceURI, ParamValue);
return tal;
}
}
}
back to the top
WebForm1.vb Inheriting System.Web.UI.Page
Imports System.Xml.Xsl
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Xml1 As System.Web.UI.WebControls.Xml
#Region " Web Form Designer Generated Code "
'The Web Form Designer requires this call.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: The Web Form Designer requires this method call.
'Do not use the Code editor to modify it.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Insert user code to initialize the page here.
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim tal As New XsltArgumentList()
tal.AddParam("calories", "", TextBox1.Text)
Xml1.TransformArgumentList = tal
Xml1.Visible = True
End Sub
End Class
back to the top
Class1.cs Inheritance Example
using System;
using System.Xml.Xsl;
namespace ClassLibrary1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1 : System.Web.UI.Page
{
public XsltArgumentList CreateArgumentList(
string ParamName, string NamespaceURI, string ParamValue)
{
XsltArgumentList tal = new XsltArgumentList();
tal.AddParam(ParamName, NamespaceURI, ParamValue);
return tal;
}
}
}
back to the top
WebForm1.vb Inheriting ClassLibrary1
Imports System.Xml.Xsl
Public Class WebForm1
Inherits ClassLibrary1.Class1
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Xml1 As System.Web.UI.WebControls.Xml
#Region " Web Form Designer Generated Code "
'The Web Form Designer requires this call.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: The Web Form Designer requires this method call.
'Do not use the Code editor to modify it.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Insert user code to initialize the page here.
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Xml1.TransformArgumentList = CreateArgumentList("calories", "", TextBox1.Text)
Xml1.Visible = True
End Sub
End Class
back to the top
Verify That It Works
- Press the F5 key to run the application in debug
mode.
- The entire menu appears by default. Take note of the
calorie counts for each item.
- In the text box, type a number of calories, and then click Filter Menu. Notice that only those menu items that are less than or equal to
this calorie amount appear.
- Press the SHIFT+F5 key combination to stop debugging and to
return to Visual Studio.
back to the top
Steps to Demonstrate Multilanguage Inheritance
To illustrate multilanguage inheritance, you must modify the Web
Application project. Instead of creating an instance of the Class Library
component, the application inherits from the Visual Basic .NET Web Form
code-behind class.
The code-behind class currently inherits the
System.Web.UI.Page class. Because Microsoft .NET does not support multiple
inheritance, you must move
Page class inheritance to the Visual C# class.
- Add a reference to the System.Web namespace in the Class Library before you modify the Visual C#
code.
- In the Solution Explorer window, under ClassLibrary1, right-click References, and then click Add Reference.
- On the .NET tab, double-click System.Web.dll. Notice that System.Web.dll appears in the Selected Components list box.
- Click OK. Notice that System.Web appears under References in your Class Library.
- Modify the Class1 declaration to inherit the Page class:
public class Class1 : System.Web.UI.Page
- The Visual Basic .NET or Visual Basic 2005 code-behind class is now ready to
inherit Class1. Because Class1 inherits the Page class, the code-behind class also inherits the Page class through its inheritance of Class1.
In the Editor window, click WebForm1.aspx.vb. Under the class declaration, replace System.Web.UI.Page with ClassLibrary1.Class. The updated code should appear as follows:
Inherits ClassLibrary1.Class1
- Comment out the first line in the Click event handler as follows:
'Dim myArgList As New ClassLibrary1.Class1()
- Modify the second line in the Click event handler by calling CreateArgumentList without an object reference:
Xml1.TransformArgumentList = CreateArgumentList("calories", "", TextBox1.Text)
- Repeat the steps in the Verify That It Works section.
back to the top
Troubleshooting
back to the top
REFERENCES
For more information, refer to the following Microsoft Web sites:
back to the top