c# - 在 WPF 中解析本地 XML 文档(调试工作,发布后失败)

标签 c# wpf xml

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

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

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

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

这是我的 XmlReader 代码的一小段供引用:

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>

最佳答案

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

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

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

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

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

关于c# - 在 WPF 中解析本地 XML 文档(调试工作,发布后失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/256529/

相关文章:

c# - 如何在运行时创建包含类的 dll?

C# 读取文本文件,换行

c# - ASP.NET ScriptManager 输出未包含在 ASP.NET 部分缓存 (ascx) 中

C# 如何从另一个线程关闭主 UI 线程上的窗口窗体

python - 通过使用 Python 修改 Excel 后面的 xml 来动态更改 Excel 中的文件共享根目录

C# 探测并捕获 Javascript Alert() 和 Confirm()

WPF 验证错误

c# - 合并的 ObservableCollection

xml - 如何仅使用 XSLT 提取 "for eached"元素的子元素

android - 如何在 android studio 中按字母顺序格式化 XML 代码?