Introduction
Recently, I was working on AdRotator control in Asp.Net 2.0 at one stage while adding an Ads.xml file.
I noticed the following error.
Error
The AdRotator adRtrCtrl/adRotator1 could not find the AdvertisementFile or the file was invalid.
Possible causes
- Check the XML file to see if it is in well format.
- Check <NavigateUrl> tag if it is of the form http://www.csharpcorner.com/adv=Y&ID=123.
The ampersand & is a reserved charactor in XML, so you have to use &.
- If the nature of your XML file is something of the form as listed below.
<Advertisements>
<Adv>
<ImageUrl>Images/1.gif</ImageUrl>
<NavigateUrl>http://www.c-sharpcorner.com</NavigateUrl>
<Alt>Csharp code</Alt>
<Caption>Csharp Site</Caption>
</Adv>
</Advertisements>
If the first element in the above file is other than <Ad>, then it also gives an error. It has to have the very first child element as <Ad>; this is what I have observed with the code.
Example
<Advertisements>
<Ad>
<ImageUrl>Images/1.gif</ImageUrl>
<NavigateUrl>http://www.c-sharpcorner.com</NavigateUrl>
<Alt>Csharp code</Alt>
<Caption>Csharp Site</Caption>
</Ad>
</Advertisements>
Code for Default. aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: left">
<table>
<tr>
<td style="width: 259px">
<asp:Label ID="Label1" runat="server" Font-Names="Verdana" Font-Size="Smaller"
Text="AdRotator Component randomly picks up ad banners from XML file and clicking on banner takes to the specified URL"
Width="301px"></asp:Label>
</td>
<td style="width: 496px">
<asp:AdRotator ID="adRtrCtrl" Target="_self" runat="Server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code for Ads.xml file
<Advertisements>
<Ad>
<ImageUrl>Images/1.gif</ImageUrl>
<NavigateUrl>http://www.c-sharpcorner.com</NavigateUrl>
<Alt>Csharp code</Alt>
<Caption>Csharp Site</Caption>
</Ad>
<Ad>
<ImageUrl>Images/1.gif</ImageUrl>
<NavigateUrl>http://www.vbdotnetheaven.com</NavigateUrl>
<Alt>VB.Net</Alt>
<Caption>Visual Basic</Caption>
</Ad>
<Ad>
<ImageUrl>Images/3.gif</ImageUrl>
<NavigateUrl>http://www.c-sharpcorner.com</NavigateUrl>
<Alt>Csharp code</Alt>
<Caption>Csharp Site</Caption>
</Ad>
<Ad>
<ImageUrl>Images/4.gif</ImageUrl>
<NavigateUrl>http://www.mindcracker.com</NavigateUrl>
<Alt>.Net Professional Job Site</Alt>
<Caption>VS.Net</Caption>
</Ad>
</Advertisements>
Note. I have tested the code 5-6 times with the above error.