间歇性地不为c#解决方案加载XML文件

时间:2015-11-04 21:36:35

标签: c# xml visual-studio-2015 vsto

编辑:附加的屏幕截图显示返回的错误。这是一个找不到目录的错误。在"消息"。

之后出现例外细节

enter image description here

我使用Visual Studio 2015编写了一个C#VSTO应用程序。该应用程序在Microsoft Word 2013中运行(我正在使用Word在线测试)。

在表单加载事件中,我运行一些打开并读取两个XML文件的代码。这些文件包含用于填充组合框的数据。每个XML文件都有一个设置为Copy Always。

的属性

问题是XML文件有时无法加载。如果我再次运行代码,XML文件会加载。大多数情况下(80%)没有错误,XML文件按预期运行。其他时候,XML文件未加载。

这些xml文件位于项目文件夹中,子文件夹为" xmlfiles"。这些文件非常小,大约每个2k,不超过20行元素,所以如果出现超时问题我会感到惊讶。

你知道为什么应用程序会在大多数时间加载XML文件,但不是所有时间都加载吗?

以下是加载文件的代码:

public void GetBookLanguages()
{
    // This application has a combobox that displays a list of languages Amazon supports.
    // the user can select from this list or enter their own.

    string xmlFile = string.Format(@"xmlfiles\\BookLanguages.xml");

    try
    {
        XDocument xmlDoc = XDocument.Load(xmlFile);
        foreach (var language in xmlDoc.Descendants("item"))
        {
            string id = language.Attribute("id").Value;
            string description = language.Value;
            bookLanguages.Add(new BookLanguageTypes()
            {
                bookLangShortName = id,
                bookLangDescription = description
            });
        }

    }
    catch {
        MessageBox.Show("Warning: Could not load the BookLanguages.xml file that contains a list of " +
    "available languages. This information would normally display in a pick list. You can stil run this program.");
    }
}

以下是相应的XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<Items>
  <item id="en">English (en)</item>
  <item id="de">German (de)</item>
  <item id="fr">French (fr)</item>
  <item id="it">Italian (it)</item>
  <item id="es">Spanish (es)</item>
  <item id="zh">Chinese (zh)</item>
  <item id="ja">Japanese (ja)</item>
  <item id="pt">Portuguese (pt)</item>
  <item id="ru">Russian (ru)</item>
  <item id="nl">Dutch (nl)</item>
</Items>

您可以提供的关于为什么这些XML文件并不总是加载的任何见解都会有所帮助。

0 个答案:

没有答案