タイトルはすべて本当にそう言います。私は数日間この1つに固執されていくつかの助けに感謝します。メインページと設定ページは、ローカルフォルダ内のsettings.xmlを初めてテストし、見つからなければコピーします。ユーザーが設定ページを開くと、ローカルフォルダから詳細をロードして、OnNavigatedFromイベントからローカルフォルダに保存する前に編集できるようになります。uwpアプリケーションでxmlを読み込み、編集して保存する方法
インストールフォルダからローカルフォルダに設定ページをロードして保存する
// Has the file been copied already?
bool blFileExist = false;
try
{
await ApplicationData.Current.LocalFolder.GetFileAsync("settings.xml");
// No exception means it exists
blFileExist = true;
btnSettings.Foreground = new SolidColorBrush(Windows.UI.Colors.White);
}
catch (System.IO.FileNotFoundException)
{
// The file obviously doesn't exist
blFileExist = false;
btnSettings.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
}
catch (Exception)
{
}
if (!blFileExist)
{
try
{
// Cant await inside catch, but this works anyway
StorageFile stopfile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("settings.xml");
await stopfile.CopyAsync(ApplicationData.Current.LocalFolder);
}
catch (System.IO.FileNotFoundException)
{
}
catch (Exception)
{
}
}
コード
private void loadSettings()
{
try
{
doc = XElement.Load("settings.xml");
nAIPlayers = int.Parse(doc.Element("ai_players").Value);
strCardBack = doc.Element("back").Value;
comboBoxAIPlayers.SelectedIndex = nAIPlayers - 1;
}
catch (System.IO.FileNotFoundException)
{
}
catch (Exception ex)
{
}
}
private async void saveSettings()
{
//try
//{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("settings.xml");
using (Stream fileStream = await file.OpenStreamForWriteAsync())
{
doc.SetElementValue("ai_players", nAIPlayers);
doc.SetElementValue("back", "Back_0");
doc.Save(fileStream);
}
/*}
catch (System.IO.FileNotFoundException)
{
}
catch (Exception ex)
{
}*/
}
をロードするためのコード私はこの問題は、私はそれを保存するために、ローカルファイルへのアクセスだと思いとそれをロードするインストールファイル。結果は私がそれを保存しても元のsettings.xmlの値を読み込みます
これをローカルフォルダからロードするにはどうしたらいいですか?
doc = XElement.Load( "settings.xml");
更新
最初の反復でコードが正常に動作し、それが必要として設定ページのコードが表示されます。設定ページを終了してsaveSettings()メソッドを実行した後にのみ失敗し、設定ページを再ロードしてloadSettings()を実行するとエラーがスローされます。
System.Xml.XmlException:ルートレベルのデータが無効です。 5行目、12位
ありがとうございました。私はそれを多く更新しました。 – luigivampa
@luigivampa間違ったXMLファイルを書いているようですので、データが間違っています。最高です - それをデバッグし、KnownFolderにそのファイルを保存/コピーして見た目を確認するか、[ISEを使う](http://stackoverflow.com/a/33063501/2681948) - どういうわけかファイルを開き、何が間違っているかを見てください。 – Romasz