Reading an XML resource file in a Javascript WinRT app

6

I'm trying to set up a config file for some app-specific items (such as API keys) for my WinRT experiment.

So far I added a "config.xml" file to the root of my project, marked it as a resource in the properties... and then I'm stuck.

Every example I can find seems to deal with JSON resource files (which are somehow tied to localization by convention and don't seem to be appropriate for general config stuff?), loading files from disk (which doesn't work since resources get compiled into the .pri file), or uses C#.

So how can I make this work in my Javascript/HTML5 app?

My latest attempt was this:

var uri = new Windows.Foundation.Uri('ms-resource:///config');
var xml = Windows.ApplicationModel.Resources.ResourceLoader
                                            .getStringForReference(uri);

but this doesn't work and throws the following exception:

0x80073b1f - JavaScript runtime error: ResourceMap Not Found.

What am I missing?


I feel like I'm getting closer:

var r = Windows.ApplicationModel.Resources.Core.ResourceManager
               .current.mainResourceMap.lookup('Files/config.xml');
var candidates = r.resolveAll();
candidates[0].getValueAsFileAsync().done(readXml);

This finds the file resource, but all the candidate contains is the original absolute path to the file, so getValueAsFileAsync() throws an exception saying "The system cannot find the file specified." (and the readXml callback isn't called).

So I still can't seem to get at the contents of this XML file.

javascript
windows-8
windows-runtime
embedded-resource
asked on Stack Overflow Sep 27, 2012 by Adam Lear • edited Sep 27, 2012 by Adam Lear

1 Answer

4

Try this...

var uri = new Windows.Foundation.Uri('ms-appx:///myFolder/myConfig.xml');
var file = Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri);

You can now use the file to parse and handle the data as needed.

answered on Stack Overflow Sep 27, 2012 by Jeff Brand

User contributions licensed under CC BY-SA 3.0