Extending the ASP.NET XML control






4.10/5 (9 votes)
Jul 17, 2002
4 min read

156200

1580
Extending ASP.NET XML control
Contents
Introduction
This project shows how to extend the ASP.NET XML control and the way to use it to call a web service. The Web Service used is sending back the latest article posted on CodeProject. The XML control delivered by Microsoft does not permit to access distant server to get the XML document. Adding this possibility to the control add new horizon to it. Most of Web Services may be called using HTPP GET, so with this new control you will be able to get information from them. The XML document returned by the Web Service will be still processed using XSLT like for the original control.
Extend the control
Using Visual Studio .NET create a new C# 'ASP.NET Web Application' project, call it
AspXmlDemo. When it is created add to the solution a new C# 'Web Control Library' project,
called WebControlXml.
After project's creation Visual Studio .NET presents the generated class. By default, Visual
Studio .NET creates a class inheriting from System.Web.UI.WebControls.WebControl
.
You need to change it to System.Web.UI.WebControls.Xml
to inherit from the ASP:XML
control.
Then change the class name from WebCustomControl1 to XmlEx. At the same time change the class
attribute to [ToolboxData("<{0}:XmlEx runat=server></{0}:XmlEx>")]
.
Finally add the namespace attribute: [assembly:TagPrefix("WebControlXml","XmlEx")]
.
You have then :
[assembly:TagPrefix("WebControlXml","XmlEx")]
namespace WebControlXml
{
/// <summary>
/// ASP:XML Extended control.
/// </summary>
[ToolboxData("<{0}:XmlEx runat=server></{0}:XmlEx>")]
public class XmlEx : System.Web.UI.WebControls.Xml
{
The URL of the XML file to download will be stored in a property of the XmlEx
class.Replace :
private string text;
byprivate string documentUrl;
In order to access this variable we add the get and set.public string DocumentUrl
{
get
{
return documentUrl;
}
set
{
documentUrl = value;
WebRequest req = WebRequest.Create(documentUrl);
WebResponse resp = req.GetResponse();
XmlTextReader reader = new XmlTextReader(resp.GetResponseStream());
this.Document = new XmlDocument();
this.Document.Load(reader);
}
}
The property get is simple but the property set need some explanations. The parameter value
is assigned to the private local variable. Then we created a WebRequest
object
that permits to read the document giving it URL. The response, in fact the XML document, is
downloaded and assigned to the parent variable from System.Web.UI.WebControls.Xml
class. Now that we have the code needed to get and set the URL to the property of the class, it
would be good to be able to set the value directly from the IDE. To achieve it you need to add
the attribute on the DocumentUrl
property: [Category("Behavior"), DefaultValue("")]
.We get then :
[Category("Behavior"), DefaultValue("")]
public string DocumentUrl
{
get
{
You need then to add, to be able to use WebRequest
and XmlDocument
:
using System.Net;
using System.Xml;
Then add to WebControlXml project a reference to the .NET assembly 'System.Xml.dll'.
We do not use the Render method. So we delete it. Save and compile.
Using the extended control
For the moment the ASP.NET application is empty so if you run the project with CTRL+F5 you get an empty page.
Open the file WebForm1.aspx from the AspXmlDemo project. Right click in the Toolbox, menu opens, click on Customize Toolbox. Click on the .NET Framework Components tab in the dialog that opens, then on the browse button. Enter the path to the assembly 'WebControlXml.dll'. Click OK. The XmlEx control is added to the Toolbox.
Drag and drop the XmlEx
control on your ASP.NET page WebForm.aspx. Browsing the property of
the control you remark that the property DocumentUrl
is added.
We will use this property to get the latest ten article summaries from CodeProject website.
You must fill the DocumentUrl
property with this url:
http://www.codeproject.com/webservices/latest.asmx/GetLatestArticleBrief?NumArticles=10
Run the application with CTRL+F5. You get an Internet Explorer window with a list of information. The Web Service called returns the latest ten article summaries from CodeProject as a XML stream. We did not specify any XSLT file so the control just output to Internet Explorer the XML file.
Add a new XSLT element to the AspXmlDemo project and name it 'codeproject.xslt'. Then modify it to get :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:cprj="http://codeproject.com/webservices/">
<xsl:output method="html" version="4.0" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="cprj:ArticleBrief" >
<p><font style="FONT-SIZE: 8pt; FONT-FAMILY: Arial, Tahoma" >
<a HREF="{cprj:URL}" target="_blank">
<b><xsl:value-of select="cprj:Title"/></b>
</a>
by <xsl:value-of select="cprj:Author"/><br/>
<xsl:value-of select="cprj:Description"/>
</font></p>
</xsl:template>
</xsl:stylesheet>
Save the file. Then modify to 'codeproject.xlt' the 'TransformSource' property of the XmlEx control
on the WebForm1.apsx page.
Run the application with CTRL+F5. You get the same information then before but with a better
formatting (sorry but I am not a WebDesigner ;).
Conclusion
We have seen that with some lines of C# code we considerably extended the possibilities offered by the ASP:XML control. This little extension opens new doors to the content representation on the Web by making it able to call Web Services. It also permits Web Designer to use Web Services without knowing all the plumbing. In fact for the Web Designer, the important thing to know is the URL to get the information and it structure. After that it needs to display them nicely.
Known Issues
- None.
Version
Version 1.00 |
15 July 2002
First release. |