Reading a Server Side XML File on Windows Azure

A question that I got at PDC was whether or not you could read an XML file contained in your Web Role when running on Windows Azure as some folks use this for metadata such as a site map.  The short answer is "yes", just as you would any ASP.Net Web Application (using MapPath to get at the file).

As an example, you could use a DataSet to read an XML file you have in the App_Data folder:

 DataSet games = new DataSet();
games.ReadXml(MapPath("App_Data/VideoGames.xml"));

gameView.DataSource = games;
gameView.DataBind();

Where gameView is defined as:

 <asp:GridView ID="gameView" runat="server"/>

Likewise, you can use an XmlDataSource to do the same thing:

 <asp:XmlDataSource ID="gameSource" DataFile="~/App_Data/VideoGames.xml" runat="server" />
<asp:GridView ID="gameView2" DataSourceID="gameSource" runat="server" />

Your XML file can reside in App_Data, or it could be at the root and everything will work as you expect -- I've tried this on the local Development Fabric as well as on Windows Azure to verify.

That said, if you look at the Windows Azure trust policy you'll see that you can't write to files in App_Data however. 

If you really have a need to reference a local store, say for caching or something, you can use the RoleManager.GetLocalResource() method.  You do so by setting the <LocalStorage/> tag in the Service Definition file (csdef) found in the Cloud Service project (ccproj)

 <LocalStorage name="tempStorage" sizeInMB="1"/>

Then in your server side code, you can access that local storage like this:

 ILocalResource locRes = RoleManager.GetLocalResource("tempStorage");

Where ILocalResource gives you a path where you can read/write files -- just be aware that you can't count on any files you write to always be there as the Fabric could stop your Role Instance and spin it up on a different VM, any kind of state should be stored in Windows Azure storage.

That is, If you data that is changing, use Blob or Table Storage.  If you have a large amount of static data, and want to cache a subset locally for better performance, you can use Local Storage.