loadFromFileAync(), where does it end up loading the DOM from the file?


  • Hi,

    I want to load a variable with the contents of a local XML file.

    From the documentation I got this description:

    var promise = Windows.Data.Xml.Dom.XmlDocument.loadFromFileAsync(file, loadSettings).then( /* Your success and error handlers here */ )

    So, I want to parse the loaded DOM inside the "success" handler. My question is:

    1. Where is the actual DOM being stored once it has been read from the file? I need to access the DOM inside the success handler, so I need to specify where I want the DOM to be loaded.

    2. The "file" parameter being passed to loadFromFileAsync is supposed to be of type "IStorageFile", but the documentation

    describes this as an "interface" in C#, C++ and VB, so what does it mean for JavaScript?


    If there is an example on how to use the loadFromFileAsyn() to laod a local file, that would be helpful.




    Monday, December 05, 2011 7:06 PM


All replies

  • Hi mesarina,

    We could get the file via the method StorageFile.GetFileFromPathAsync | getFileFromPathAsync, or we could access the files in the KnownFolders via method:

    (or other KnownFolders in system from http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.knownfolders)


    And do not forget to add the for File Type Associations (.xml) in package.appxmanifest if you want to read one *.xml file. 



    My sample here: https://skydrive.live.com/#cid=51B2FDD068799D15&id=51B2FDD068799D15%21874



    Metro App SDK Samples > File access sample my help you: http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597  and one Build session: http://channel9.msdn.com/Events/BUILD/BUILD2011/PLAT-282T


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bob_Bao Tuesday, December 06, 2011 9:55 AM
    • Proposed as answer by Bob_Bao Wednesday, December 07, 2011 10:30 AM
    • Marked as answer by Bob_Bao Monday, December 12, 2011 6:34 AM
    Tuesday, December 06, 2011 9:48 AM
  • Thank you very much Bob.

    I have one quesiton though, why does the documentation only describes the parameters used in the first part (method part) of a 'method().then(function( param1, param2,.. )' asynchronous function, but it doesn't say anything about the "function(param1, param2,..)" of the "then" part?

    When I go and look at the "loadFromFileAsync()" description for example, this is what I get from the documentation:

    var promise = Windows.Data.Xml.Dom.XmlDocument.loadFromFileAsync(file, loadSettings).then( /* Your success and error handlers here */ );

    Your code was very helpful because you showed that in the 'then' method I can call  a function with a parameter that holds the XML contents of the file that was loaded. Like this:

    var promise = Windows.Data.Xml.Dom.XmlDocument.loadFromFileAsync(file, new Windows.Data.Xml.Dom.XmlLoadSettings()).then(function(xml)) {



    So, had i not seen your code, I would not know that the 'promise' part of loadFromFileAsync method, returns the 'xml' contents in the 'xml' parameter in the function(xml).

    So I would like to know how am I supposed to know that the promise part of the loadFromFileAsync method returns the 'xml' content into a parameter that I can use in the 'function' call inside the 'then' part?

    Maybe I am not reading the documentation right? I would like to know for future reference, where in the description of an asynchronous method, I should expect to find information about the 'then' parameters that are returned as part of the promise?



    Tuesday, December 06, 2011 7:51 PM
  • MSDN documents for Metro just are preliminary now, we will change them in future.

    For the async method in JS, we can handle three functions in then(), one for success, one for error, and the last one is for progress. But for the parameter in the first success function, you should view the return type of the async function. For example, loadFromFileAsync returns XmlLoadOperation type, and it inherits from IAsyncOperation<XmlDocument>. So we should know the success function owns one parameter: XmlDocument type.

    For the 'promise' return object, it just provide the asynchronous operation, that could use it to do "then" or "cancel" in JS.

    Please read Asynchronous patterns in the Windows Runtime (it contains the JS part).


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bob_Bao Wednesday, December 07, 2011 10:33 AM
    Wednesday, December 07, 2011 10:30 AM