Click here to Skip to main content
6,594,932 members and growing! (15,560 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Code Project Open License (CPOL)

Databaseless Xml/Xslt web Advertiser

By lamoureternal

Host Advertises on your web sites without database using xml/xslt
C# (C# 1.0, C# 2.0, C# 3.0), XML, XSLT, .NET (.NET 2.0), ASP.NET, CEO, Architect, DBA, Dev, QA, Design
Posted:4 Apr 2008
Views:5,990
Bookmarked:12 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.77 Rating: 2.54 out of 5

Download Advertiser - 44.3 KB

Introduction

lately I've been playing with xml/xslt transformation and just noticed how easy I can produce whatever contents from plain xml files, overriding my traditional time-consuming technique of parsing xml, I decided to play a lettle pit with my Advertiser module, which can be of use on websites when you want to host people's advertises. this module should do it without database at all.

Using the code

First here's the structure of the sample website:

Advertiser(root)
|
|--->Admin [Amin area]
| |
| |--->Advertiser
| |
| |--->Add.aspx [add files to locations]
| |
| |--->Default.aspx [display/edit/delete files]
| |
| |--->Edit.aspx [edit files]
|
|
|---->App_Code
| |
| |--->Advertiser
| |
| |--->AddevrtiserManager.cs
|
|
|----->SiteScripts
| |
| |--->flashfix.js [used to render flash]
|
|---->uc
| |
| |--->Advertiser.ascx [user control displays the transformed file]
|
|
|---->UploadedData [+r/+w permission required]
| |
| |---->Advertiser
| |
| |--->config [Configuraion files]
| | |
| | |--->Advertiser.xml
| | |--->Advertiser.xslt
| |
| |----> [Ad. files]
|
|---->Default.aspx [Test page]

The Xml file looks like this :

<?xml version="1.0" encoding="utf-8"?>
<Advertiser>
  <addvert type="Front_Content_Bottom">
    <item id="4cf7f1f6-614a-4810-88f6-c73d681e9e61">
      <type>FlashMovie</type>
      <path>/UploadedData/Advertiser/4cf7f1f6-614a-4810-88f6-c73d681e9e61.swf</path>
      <width>500</width>
      <height>100</height>
    </item>
  </addvert>...

<xsl:param name="ExternalParameter"></xsl:param>
<xsl:param name="AdvType">AnimatedGif</xsl:param>
<xsl:template match="/">
<xsl:apply-templates select="Advertiser/addvert"/>
</xsl:template>
<xsl:template match="addvert">
 <xsl:choose>
  <xsl:when test="@type=$ExternalParameter">
   <table cellpadding="0" cellspacing="0" border="0" id="tbl">
    <xsl:for-each select="item">
     <tr>
      <xsl:choose>
       <xsl:when test="contains(type, $AdvType)">
        <td>
         <a target="_blank">
           <xsl:if test="string-length(normalize-space(url)) > 0">
             <xsl:attribute name="href">
              <xsl:value-of select="url" />
             </xsl:attribute>
          </xsl:if>
         <img border="0">
          <xsl:attribute name="src">
            <xsl:value-of select="path" />
          </xsl:attribute>
         <xsl:attribute name="width">
           <xsl:value-of select="width" />
         </xsl:attribute>
         <xsl:attribute name="height">
           <xsl:value-of select="height" />
         </xsl:attribute>
        </img>
      </a>
     </td>
   </xsl:when>
 <xsl:otherwise>
  <td>
   <script type="text/javascript">
     RenderFlash('<xsl:value-of select="path" />', <xsl:value-of select="width" />, <xsl:value-of select="height" />);
   </script>
  </td>
  </xsl:otherwise>
 </xsl:choose>
 ...............

The display page is based on a user control "Advertiser.ascx" with a property "Location" which tells the control to fetch the advertises in that belongs to this specefic location.

private Advertiser.Location _Location;
    /// 
    /// Gets or Sets Location
    /// 
    public Advertiser.Location Location
    {
        get { return _Location; }
        set { _Location = value; }
    }
    
    protected override void Render(HtmlTextWriter writer)
    {
        string type = _Location.ToString();

        XPathDocument xdoc = new XPathDocument(Context.Server.MapPath("~") + "/UploadedData/Advertiser/config/Advertiser.xml");
        XslTransform xslt = new XslTransform();
        XsltArgumentList xsltArg = new XsltArgumentList();
        xsltArg.AddParam("ExternalParameter", "", type);

        xslt.Load(Context.Server.MapPath("~") + "/UploadedData/Advertiser/config/Advertiser.xslt");
        xslt.Transform(xdoc, xsltArg, writer);
    }

While in the Admin area I decided to play dirty parsing the xml manually, to show the difference beteen the xslt style and the hard code one, although during the developement I was confronted by this annoying delima, how can I use xslt to fetch specefic location's ads and displaying them separetly in a datagrid along with eidt and delete controls.

        /// 
        /// Returns array of ads that can be used as Data source object
        /// 
        /// 
        /// 
        public static AdvertiseObject[] GetObjectsByLocation(Location location)
        {
            XmlDocument doc = GetXmlDocument();
            XmlNodeList advertises = doc.GetElementsByTagName("addvert");
            ArrayList arr = new ArrayList();
            int counter = 0;
            foreach (XmlNode advert in advertises)
            {
                if (advert.Attributes["type"].Value == location.ToString())
                {
                    foreach (XmlNode item in advert.ChildNodes)
                    {
                        counter++;
                        AdvertiseObject adv = GetAdvObjectById(new Guid(item.Attributes["id"].Value));
                        arr.Add(adv);
                    }
                }
            }
            AdvertiseObject[] res = new AdvertiseObject[counter];
            for (int i = 0; i < counter; i++)
            {
                res[i] = (AdvertiseObject)arr[i];
            }
            return res;
        }

In the user control there was no problem retrieving the ads of a location and render them directely on the control area, but I didn't manage to go arround that to feed them to a datagrid at the moment.

Points of Interest

It is quite interesting in fact how xslt simplifies the work with xml data, that is the core of this project

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

lamoureternal


Member

Occupation: Web Developer
Location: Egypt Egypt

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralThank you PinmemberTony_elvlin15:18 4 Apr '08  
GeneralRe: Thank you PinmemberMember 367673115:26 4 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Apr 2008
Editor:
Copyright 2008 by lamoureternal
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project