以下のコードを使用して、xmlファイルをインストールされた場所からローカルフォルダにロードします。xmlファイルをuwpアプリケーションにロードするC#
// Has the file been copied already?
bool blFileExist = false;
try
{
await ApplicationData.Current.LocalFolder.GetFileAsync("settings.xml");
// No exception means it exists
blFileExist = true;
}
catch (System.IO.FileNotFoundException)
{
// The file obviously doesn't exist
blFileExist = false;
}
catch (Exception)
{
}
if (!blFileExist)
{
try
{
// Cant await inside catch
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("settings.xml");
await file.CopyAsync(ApplicationData.Current.LocalFolder);
}
catch (System.IO.FileNotFoundException)
{
}
catch (Exception)
{
}
}
これを使用して、必要に応じてロードします。
private async void loadSettings()
{
try
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("settings.xml");
using (Stream fileStream = await file.OpenStreamForReadAsync())
{
doc = XElement.Load(fileStream);
}
}
catch (System.IO.FileNotFoundException ex)
{
}
catch (Exception ex)
{
}
}
最後に、これを使用してローカルフォルダに保存します。
private async void saveSettings()
{
try
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("settings.xml");
using (Stream fileStream = await file.OpenStreamForWriteAsync())
{
//Save URLs
doc.Save(fileStream);
}
}
catch (System.IO.FileNotFoundException ex)
{
}
catch (Exception ex)
{
}
}
コードはコンパイル、実行され、エラーは発生しませんが、期待どおりに実行されません。それはコードの外ステップと、エラーをスローし、それがラインに到達するcatchブロックにステッピングかのようにそれを呼び出す方法に戻り2番目のコードブロック内のコードを使用してファイルをロードするとき
using (Stream fileStream = await file.OpenStreamForReadAsync())
完全にはわかりませんが、エンコーディングとバイトオーダーマークの可能性があります。助言がありますか?
ここに問題のXMLがあります。
<?xml version="1.0" encoding="utf-8" ?>
<settings>
<feeds>
<url name="All">http://services.parliament.uk/calendar/all.rss</url>
<url name="CommonsMainChamber">http://services.parliament.uk/calendar/commons_main_chamber.rss</url>
<url name="CommonsSelectCommittee">http://services.parliament.uk/calendar/commons_select_committee.rss</url>
<url name="CommonsGeneralCommittee">http://services.parliament.uk/calendar/commons_general_committee.rss</url>
<url name="CommonsWestminsterHall">http://services.parliament.uk/calendar/commons_westminster_hall.rss</url>
<url name="LordsMainChamber">http://services.parliament.uk/calendar/lords_main_chamber.rss</url>
<url name="LordsGrandCommittee">http://services.parliament.uk/calendar/lords_grand_committee.rss</url>
<url name="LordsSelectCommittee">http://services.parliament.uk/calendar/lords_select_committee.rss</url>
<url name="ParliamentaryNews">http://www.parliament.uk/g/RSS/news-feed/?pageInstanceId=209</url>
<url name="CommonsNews">http://www.parliament.uk/g/RSS/news-feed/?pageInstanceId=25366</url>
<url name="LordsNews">http://www.parliament.uk/g/RSS/news-feed/?pageInstanceId=25367</url>
<url name="CommitteeNews">http://www.parliament.uk/g/RSS/news-feed/?pageInstanceId=25388</url>
<url name="CommonsBriefingPapers">http://researchbriefings.parliament.uk/rssfeed/Commons%20Briefing%20papers</url>
<url name="LordsLibraryNotes">http://researchbriefings.parliament.uk/rssfeed/Lords%20Library%20notes</url>
<url name="LordsInFocus">http://researchbriefings.parliament.uk/rssfeed/Lords%20In%20Focus</url>
<url name="POSTBriefs">http://researchbriefings.parliament.uk/rssfeed/POSTbriefs</url>
<url name="POSTNotes">http://researchbriefings.parliament.uk/rssfeed/POSTnotes</url>
<url name="EarlyDayMotions">http://www.parliament.uk/g/rss/generic/?pageInstanceId=78055&type=Edms</url>
</feeds>
</settings>
catchブロックに入ると、例外は何ですか? –
settings.xmlファイルの内容は何ですか? – mm8
ケン、私が言ったように、それは私が知っているどんな誤りも投げていない。 – luigivampa