I'm loading an xml file from installed location into the local folder using the code below.
// 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)
{
}
}
I then load it when necessary using this.
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)
{
}
}
Finally I save it back to the local folder using this.
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)
{
}
}
Code compiles, runs and doesn't throw any errors but doesn't do as expected. When loading the file using the code in the second code block it steps out of the code and goes back to the method that calls it as if throwing an error and stepping into a catch block when it reaches the line
using (Stream fileStream = await file.OpenStreamForReadAsync())
Not entirely sure but I think it might be down to encoding and the byte order mark. Any suggestions?
Here's the xml in question.
<?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>
I'm afraid that your issue was in your XML file. Please check the last url's value. The XML file really contains an "&" character.
<url name="EarlyDayMotions">http://www.parliament.uk/g/rss/generic/?pageInstanceId=78055&type=Edms</url>
A real "&" on it's own breaks XML files.
You could use &
in place of &
, then your code would work.
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.