将xml文件加载到uwp应用程序c#

时间:2017-02-12 15:53:49

标签: c# xml uwp byte-order-mark

我使用下面的代码将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;
        }
        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块一样

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>

1 个答案:

答案 0 :(得分:2)

我担心您的问题出在您的XML文件中。请检查最后一个网址的值。 XML文件确实包含&#34;&amp;&#34;字符。

<url name="EarlyDayMotions">http://www.parliament.uk/g/rss/generic/?pageInstanceId=78055&type=Edms</url>

真正的&#34;&amp;&#34;它自己的XML文件。

您可以使用&amp;代替&,然后您的代码就可以使用。