Click here to Skip to main content
Click here to Skip to main content

Extending the ASP.NET XML control

, 16 Jul 2002
Rate this:
Please Sign up or sign in to vote.
Extending ASP.NET XML control
<!-- Download Links --> <!-- Article image -->

Sample Image - codeproject.gif

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;
by
private 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 Wink | ;) .

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Laurent Kempé
Architect
France France

Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

He is currently employed by Innoveo Solutions since 10/2007 as a Senior Solution Architect and certified Scrum Master.

Founder, owner and Managing Partner of Jobping, which provides a unique and efficient platform for connecting Microsoft skilled job seekers with employers using Microsoft technologies.

Laurent is awarded by Microsoft since Avril 2002: Most Valuable Professional (MVP).

Follow on   Twitter

Comments and Discussions

 
QuestionHow to use single xsl file with differen xml controls Pinmemberbabunag7-Aug-07 0:16 
GeneralExtending TransformSource as well Pinmemberaalo10-Jun-03 3:04 
GeneralRe: Extending TransformSource as well PinmemberLaurent Kempé3-Aug-03 3:13 
QuestionCan use on a client application? PinmemberWeiye Chen4-Jun-03 21:31 
AnswerRe: Can use on a client application? PinmemberLaurent Kempé4-Jun-03 21:36 
GeneralRe: test PinmemberLaurent Kempé1-Jun-03 21:13 
GeneralRe: test PinmemberLaurent Kempé2-Jun-03 1:10 
GeneralNice Job! PinmemberSoliant10-Sep-02 9:47 
GeneralRe: Nice Job! PinmemberLaurent Kempé10-Sep-02 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 17 Jul 2002
Article Copyright 2002 by Laurent Kempé
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid