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

Databaseless Xml/Xslt web Advertiser

By , 4 Apr 2008
 

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;
    /// <summary />
    /// Gets or Sets Location
    /// </summary />
    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.

        /// <summary />
        /// Returns array of ads that can be used as Data source object
        /// </summary />
        /// <param name=""location"" /></param />
        /// <returns /></returns />
        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
Software Developer (Senior) EgyptNetwork
Egypt Egypt
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThank youmemberTony_elvlin4 Apr '08 - 14:18 
great work I was searching for that. thank you I also like to have click counter for each ad. thanks in advance
GeneralRe: Thank youmembervasanthamateti26 Sep '12 - 2:55 
I am unable to understand... Frown | :(
vasantha

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 4 Apr 2008
Article Copyright 2008 by lamoureternal
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid