如何在项目代码中引用Visual Studio 2012项目中的XML文件作为数据源?

时间:2021-10-25 06:43:30

My project includes a combo box with a list of databases for the user to select. The selection determines which database will be contacted later on.

我的项目包含一个组合框,其中包含用户可以选择的数据库列表。选择决定稍后联系哪个数据库。

Since databases have odd names, I'd like to give each an intelligible designation to be shown in the combo box's Display Value, with the actual database name hidden in the Value Member property (ie, "Database 1" for "XYZprojDB123"). So I put together an XML file with Display Value / Value Member pairs and added it to my project in Visual Studio, but now I can't link to it in my code. Just writing

由于数据库有奇怪的名称,我想给每个数据库指定一个可理解的名称,显示在组合框的显示值中,实际的数据库名称隐藏在值成员属性(即“XYZprojDB123”的“database 1”)中。所以我将一个带有显示值/值成员对的XML文件放在Visual Studio中,并将它添加到我的项目中,但是现在我无法在代码中链接到它。只是写

XDocument dbList = XDocument.Load("Databases.xml");

doesn't work.

是行不通的。

This is a Windows application, so I'll want to include this XML file for distribution when I publish. Does anyone know how to make this work?

这是一个Windows应用程序,因此我希望在发布时包含这个XML文件以供分发。有人知道怎么做吗?

1 个解决方案

#1


5  

If the XML file has been added to your project:

如果XML文件已经添加到您的项目:

  • Right click on your XML file in the solution explorer and click Properties
  • 在解决方案资源管理器中右键单击XML文件并单击Properties
  • change Build Action to None
  • 将构建操作更改为None。
  • change Copy to output directory to Copy if newer
  • 如果更新,则将Copy更改为output目录

Once you do that, your xml file will be copied to your project's local directory, and XDocument dbList = XDocument.Load("Databases.xml"); should work.

这样做之后,xml文件将被复制到项目的本地目录,XDocument dbList = XDocument. load(“database .xml”);应该工作。

#1


5  

If the XML file has been added to your project:

如果XML文件已经添加到您的项目:

  • Right click on your XML file in the solution explorer and click Properties
  • 在解决方案资源管理器中右键单击XML文件并单击Properties
  • change Build Action to None
  • 将构建操作更改为None。
  • change Copy to output directory to Copy if newer
  • 如果更新,则将Copy更改为output目录

Once you do that, your xml file will be copied to your project's local directory, and XDocument dbList = XDocument.Load("Databases.xml"); should work.

这样做之后,xml文件将被复制到项目的本地目录,XDocument dbList = XDocument. load(“database .xml”);应该工作。