Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET
Article

Extending the ASP.NET XML control

Rate me:
Please Sign up or sign in to vote.
4.14/5 (4 votes)
16 Jul 20024 min read 154.4K   1.6K   70   9
Extending ASP.NET XML control

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 :

C#
[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 :

C#
private string text;
by
C#
private string documentUrl;
In order to access this variable we add the get and set.
C#
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 :

C#
[Category("Behavior"), DefaultValue("")]
public string DocumentUrl 
{
    get
    {
You need then to add, to be able to use WebRequest and XmlDocument:
C#
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 :

xsl
<?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.

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


Written By
Team Leader
France France
I am an experienced Team Leader & Senior Solutions Architect with a passion for shipping high-quality products by empowering development team and culture toward an agile mindset. I bring technical vision and strategy, leading engineering teams to move product, processes and architecture forward.

Comments and Discussions

 
QuestionHow to use single xsl file with differen xml controls Pin
babunag7-Aug-07 0:16
babunag7-Aug-07 0:16 
GeneralExtending TransformSource as well Pin
aalo10-Jun-03 3:04
aalo10-Jun-03 3:04 
GeneralRe: Extending TransformSource as well Pin
Laurent Kempé3-Aug-03 3:13
Laurent Kempé3-Aug-03 3:13 
QuestionCan use on a client application? Pin
Weiye Chen4-Jun-03 21:31
Weiye Chen4-Jun-03 21:31 
AnswerRe: Can use on a client application? Pin
Laurent Kempé4-Jun-03 21:36
Laurent Kempé4-Jun-03 21:36 
GeneralRe: test Pin
Laurent Kempé1-Jun-03 21:13
Laurent Kempé1-Jun-03 21:13 
GeneralRe: test Pin
Laurent Kempé2-Jun-03 1:10
Laurent Kempé2-Jun-03 1:10 
GeneralNice Job! Pin
TigerNinja_10-Sep-02 9:47
TigerNinja_10-Sep-02 9:47 
Do you have to give the ASPNET user write privelages to the .xml and .xslt files that you want to use with the standard <asp:xml> control ?

Nice extension to a very useful control!





Soliant | email
 
"the result is that VC7 is the only compiler to generate optimized MSIL" - Stanley Lippman


GeneralRe: Nice Job! Pin
Laurent Kempé10-Sep-02 10:05
Laurent Kempé10-Sep-02 10:05 

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

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