简体   繁体   English

将XML文件加载到UWP应用程序C#

[英]Loading xml file into uwp application c#

I'm loading an xml file from installed location into the local folder using the code below. 我正在使用以下代码将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)
            {

            }
        }

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 当使用第二个代码块中的代码加载文件时,它会跳出代码并返回调用该文件的方法,就好像抛出错误并在到达该行时进入catch块

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。

<?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. 恐怕您的问题出在您的XML文件中。 Please check the last url's value. 请检查最后一个网址的值。 The XML file really contains an "&" character. XML文件实际上包含一个“&”字符。

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

A real "&" on it's own breaks XML files. 真正的“&”本身会中断XML文件。

You could use &amp; 您可以使用&amp; in place of & , then your code would work. 代替& ,那么您的代码将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM