简体   繁体   中英

How can I reference an XML file in my Visual Studio 2012 project as a data source in my project code?

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

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?

If the XML file has been added to your project:

  • Right click on your XML file in the solution explorer and click Properties
  • change Build Action to None
  • change Copy to output directory to Copy if newer

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM