我正在构建一个 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/