How to: Display Ads From an XML File Using the AdRotator Web Server Control
The AdRotator Web server control reads advertisement (ad) information from a data source, which one or more ad records. You can store ad information in an XML file and then bind the AdRotator control to the file.
All attributes of the AdRotator control are optional. The following attributes can be included in the XML files:
ImageUrl The URL of the image to display.
NavigateUrl The URL of the page to go to when the AdRotator control is clicked.
AlternateText The text to display if the image is unavailable.
Keyword The category of the ad, which can be used to filter for specific ads.
Impressions A numeric value (a weighting number) that indicates the likelihood of how often the ad is displayed. The total of all impressions values in the XML file cannot exceed 2,048,000,000 - 1.
Height The height of the ad in pixels. This value overrides the default height setting for the AdRotator control.
Width The width of the ad in pixels. This value overrides the default width setting for the AdRotator control.
To create an ad list as an XML file
Create a new XML file in your Web site's App_Data folder. For extra security, give the file a file name extension other than .xml, such as .ads.
It is recommended that you put the ad file in the App_Data folder because ASP.NET prevents files from that folder from being served to browsers.
Add the following XML elements to the file:
<?xml version="1.0" encoding="utf-8" ?> <Advertisements xmlns="http://schemas.microsoft.com/AspNet/AdRotator-Schedule-File"> </Advertisements>
Create an Ad element inside the Advertisements element for each ad that you want to include in the ad list, and then save the file.
A sample file might look like the following:
<?xml version="1.0" encoding="utf-8" ?> <Advertisements xmlns="http://schemas.microsoft.com/AspNet/AdRotator-Schedule-File"> <Ad> <ImageUrl>~/images/Contoso_ad.gif</ImageUrl> <NavigateUrl>http://www.contoso-ltd.com</NavigateUrl> <AlternateText>Ad for Contoso, Ltd. Web site</AlternateText> <Impressions>100</Impressions> </Ad> <Ad> <ImageUrl>~/images/Aspnet_ad.gif</ImageUrl> <NavigateUrl>http://www.asp.net</NavigateUrl> <AlternateText>Ad for ASP.NET Web site</AlternateText> <Impressions>50</Impressions> </Ad> </Advertisements>
To display ads from the database
Place an AdRotator control on the ASP.NET Web page at the location where you want the ads to display.
It is recommended that you use a relative URL to make the application more portable. Use syntax such as ~\App_Data\filename.ad. For information about file paths, see ASP.NET Web Project Paths.
When the page runs, the AdRotator control reads the XML file for the ads and selects one to display.