在 WPF 中解析本地 XML 文档(在调试中工作,发布后失败) [英] Parsing a local XML Document in WPF (works in debug, fails once published)

查看:29
本文介绍了在 WPF 中解析本地 XML 文档(在调试中工作,发布后失败)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a WPF application. Inside that application I am using the XmlReader class to parse several local XML files. The code I have written works perfectly during debugging, but fails once I publish the application and install it.

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

I have the XML documents as CONTENT in build action, and I have them set to COPY ALWAYS.I can confirm that the XML documents are being deployed in my build and are in tact in the application folder once installed.

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

What further confuses me is that I am using the same XmlReader code to parse RSS feeds from external websites in this application without problem. It only fails on the local XML documents.

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

Does anyone know why my XmlReader would fail to parse local XML documents once the application is published?

这是我的 XmlReader 代码的一小段供参考:

Here is a small snippet of my XmlReader code for referance:

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 安装应用程序.

By your publish description I assume you are using clickonce to install the application.

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

Clickonce has different default behavior for xml files - it assumes they are data files and places them in a different install location from your other files.

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

Please double check that your xml files really are being installed where you think they are.

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

In the publish settings you can change the setting for each xml file from data file to include. Your other files will already be set to include.

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

Note that the publish settings are independent of the build settings for the file.

这篇关于在 WPF 中解析本地 XML 文档(在调试中工作,发布后失败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆