【问题标题】:Parsing a local XML Document in WPF (works in debug, fails once published)在 WPF 中解析本地 XML 文档(在调试中工作,发布后失败)
【发布时间】:2008-11-02 04:51:01
【问题描述】:

我正在构建一个 WPF 应用程序。在该应用程序中,我使用 XmlReader 类来解析几个本地 XML 文件。我编写的代码在调试期间完美运行,但在我发布应用程序并安装后就失败了。

我在构建操作中将 XML 文档作为 CONTENT,并将它们设置为 COPY ALWAYS。我可以确认 XML 文档正在我的构建中部署,并且在安装后完好无损地保存在应用程序文件夹中。

让我更加困惑的是,我在这个应用程序中使用相同的 XmlReader 代码来解析来自外部网站的 RSS 提要没有问题。它只在本地 XML 文档上失败。

有谁知道为什么我的 XmlReader 在应用程序发布后无法解析本地 XML 文档?

这是我的 XmlReader 代码的一个小 sn-p 供参考:

XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;
            settings.IgnoreWhitespace = true;
            try
            {
                settingsReader = XmlReader.Create("Resources/data/TriviaQuestions.xml", settings);

                nodeNum = 0;
                while (settingsReader.Read())
                {
                    switch (settingsReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (settingsReader.HasAttributes)
                            {
                                for (int i = 0; i < settingsReader.AttributeCount; i++)
                                {
                                    settingsReader.MoveToAttribute(i);
                                    _feeds[nodeNum] = settingsReader.Value.ToString();
                                }
                                settingsReader.MoveToContent(); // Moves the reader back to the element node.
                            }
                            break;
                        case XmlNodeType.Text:
                            _questions[nodeNum] = settingsReader.Value;
                            nodeNum++;
                            break;

                    }

                }
                settingsReader.Close();
            }
            catch
            {

            }

这是我的 XML

<?xml version="1.0" encoding="utf-8" ?>
<Questions>
    <Question feed="http://entertainment.msn.com/rss/topboxoffice/">What movie has the top box office sales in the US right now?</Question>
    <Question feed="http://entertainment.msn.com/rss/topdvdrentals/">What is the top DVD rental in the US this week?</Question>
    <Question feed="http://entertainment.msn.com/rss/topalbums/">Which of the following albums is currently topping the charts?</Question>
</Questions>

【问题讨论】:

  • 这将有助于准确了解正在发生的故障类型,以便我们知道要寻找什么。

标签: c# wpf xml


【解决方案1】:

根据您的发布描述,我假设您正在使用 clickonce 安装应用程序。

Clickonce 对 xml 文件有不同的默认行为 - 它假定它们是数据文件并将它们放置在与其他文件不同的安装位置。

请仔细检查您的 xml 文件是否确实安装在您认为的位置。

在发布设置中,您可以将每个 xml 文件的设置从数据文件更改为包含。您的其他文件已经设置为包含。

请注意,发布设置独立于文件的构建设置。

【讨论】:

    【解决方案2】:

    你能描述一下“但是一旦我发布应用程序并安装它就会失败”?

    如果您能描述什么不起作用、错误是什么并提供异常信息,将会很有帮助。


    与您的问题无关,每次我在生产代码中看到空的 catch 块时,我都会感到恐惧!

    【讨论】:

    • 空catch块是因为这是一个sn-p代码,错误处理函数调用在这个问题的上下文中并不重要。
    【解决方案3】:

    考虑使用 IsolatedStorage 来存储您的设置,而不是在相对的 Resources 目录中。这将为您提供不同安装方案(例如 ClickOnce 安装)的已知位置。

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 2016-10-07
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多