简体   繁体   中英

How to read data from XML resource in Windows Store (Metro Style) app?

I have a windows store app. I have a XML file containing data. I need to add this file as resource of my app. I need to read data to XDocument from this file.

1) What build action I should set when add XML file to project? (I think this is "Content")

2) How to get XDocument object from this XML file?

After 2 hours I've got this code:

public static class DataLoader {
    public static XDocument LoadFromXmlResource(string path){
        path.Replace('\\', '/');
        path.TrimEnd('/');
        string uriPath = "ms-appx:///MyApp/" + path;
        Task<StorageFile> operation = StorageFile.GetFileFromApplicationUriAsync(new Uri(uriPath)).AsTask<StorageFile>();
        StorageFile file = operation.Result;
        Task<string> fileReadingTask = FileIO.ReadTextAsync(file).AsTask<string>();
        string fileContent = fileReadingTask.Result;
        XDocument result = XDocument.Parse(fileContent, LoadOptions.None);
        return result;
    }
}

this works, but I'm not shure that it is correct.

I have no foun another way. You can try find another.

This is the correct way to reference content in your application. You can also add a .resw file to your project to store resources for loading at runtime, via ResourceLoader.

Resources differ since they are designed to be culture-sensitive, so they're good for translations of button text values etc.

If you don't need this functionality, then assets added with the Build Action "Content" are perfect and you use the ms-appx or ms-appdata URI schemes to address them.

Package content via ms-appx

http://msdn.microsoft.com/en-us/library/windows/apps/Hh781215.aspx

Mutable data via ms-appdata

http://msdn.microsoft.com/en-us/library/windows/apps/hh464917.aspx

-and-

http://msdn.microsoft.com/en-us/library/windows/apps/Hh781229.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM