Robert White
Robert White

Reputation: 1

C# Silverlight - XmlDictionary from Uri

I've been developing a Silverlight application for a company's website and have encountered a problem.

Up until now I have been programming this locally, now I need to publish the program onto the website; the issue is that FileStream can only access local files with elevated permissions.

Here's a snippet of code:

using (FileStream fileStream = new FileStream(@"E:\Users\LUPUS\Documents\Visual Studio 2010\Projects\Lycaon5\Lycaon5\acids.xdb", FileMode.Open))
 {
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fileStream, XmlDictionaryReaderQuotas.Max))
  {
    //Read the XML file out.
  }
 }

Without changing anything to do with XmlDictionaryReader reader - How could I go about reading the files from a relative Uri?

Many Thanks,

Rob.

P.s. Apologies for the lack of formatting, me cave man, me don't know how.

Upvotes: 0

Views: 340

Answers (1)

Chris Sinclair
Chris Sinclair

Reputation: 23208

Embed the file with your assembly when you compile. Then you can read it as a resource stream: http://msdn.microsoft.com/en-us/library/cc296240%28VS.95%29.aspx

(Make sure you embed your file as type Resource for this method to work)

Your code might look something like this to get the file stream:

Uri uri = new Uri("/MyAssemblyName;component/acids.xdb", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(uri);
Stream fileStream = streamInfo.Stream;

using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fileStream, XmlDictionaryReaderQuotas.Max))
{
    //Read the XML file out.
}

EDIT: As Tim S pointed out, this is only good for if the data file is static and fixed at compile time. Otherwise you can use other methods to download the file from a website (as he linked to).

Upvotes: 1

Related Questions