私はWindowsストアアプリを持っています。データを含む XML ファイルがあります。このファイルをアプリのリソースとして追加する必要があります。このファイルから XDocument にデータを読み取る必要があります。
1) XML ファイルをプロジェクトに追加するときに、どのビルド アクションを設定する必要がありますか? (これは「コンテンツ」だと思います)
2) この XML ファイルから XDocument オブジェクトを取得する方法は?
2時間後、私はこのコードを手に入れました:
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;
}
}
これは機能しますが、それが正しいかどうかはわかりません。