views:

102

answers:

3

When using an XmlDataSource is there good way to handle exceptions that are caused when the remote XML file is unavailable? I'm somewhat new to .NET and using C#.

+1  A: 

It's really up to you to determine what is suitable for your application when an exception like this is raised. The only thing you shouldn't do is ignore it.

Options you have include:

  • Automatically retry a number of times, in case the connection problem is transitory
  • Return an appropriate error message to the user and perhaps log or email the exception
  • Use a previously cached version of the XML file until a fresh copy can be fetched
  • Let the exception bubble up to the calling layer and let it deal with it (perhaps logging it first)

One thing you may also need to do is clean up any resources (e.g. open connections) in a Finally block.

Dan Diplo
A: 

I tried the approach of catching the exception to trigger different handling, but it doesn't work for some reason. Instead of the exception causing the ErrorMessage to show and the Repeater1 to be hidden, I still just get an exception that kills the page: Exception Details: System.Net.WebException: The remote server returned an error: (404) Not Found.

Why can't I catch the exception and then take a different action?

protected void Page_PreRender(object sender, System.EventArgs e)
{
    try
    {
        RssSource.DataFile = "http://www.example.com/rss/feed/index1.aspx";
        RssSource.XPath = "/rss/channel/item[position()<3]";
        RssSource.EnableCaching = true;
        RssSource.CacheDuration = 43200;
        RssSource.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
    }
    catch (Exception ex)
    {
        ErrorMessage.Visible = true;
        Repeater1.Visible = false;

    }
}

<asp:Label ID="ErrorMessage" runat="server" Text="News not unavailable" Visible="false" /> 
<asp:XmlDataSource ID="RssSource" runat="server" />
<asp:Repeater ID="repeater1" runat="server" DataSourceID="RssSource">
    <ItemTemplate>
        <p><%# XPath("description")%></p>
    </ItemTemplate>
</asp:Repeater>  
itsatrp
A: 

I'm assuming this has been resolved, but am answering it in case someone else has the issue and stumbles upon this post.

You need to also bind to your XmlDataSource within your try block...

            try
            {

                xdsRSS.DataFile = Configuration.BeaconConfigurationSection.Current.SyndicatedJobs.RssUrl;
                xdsRSS.XPath = Configuration.BeaconConfigurationSection.Current.SyndicatedJobs.XPath;
                xdsRSS.EnableCaching = true;
                xdsRSS.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
                xdsRSS.CacheDuration = 6000;
                dlRSS.DataSource = xdsRSS;
                dlRSS.DataBind();
            }
            catch
            {
                dlRSS.Visible = false;
                pnlLinkToJobSite.Visible = true;
            }

...make sure to remove the DataSourceID property from your repeater, and you should be good to go.

Pattrick King