将XML文件添加到构建中 [英] Adding XML Files to the Build

查看:146
本文介绍了将XML文件添加到构建中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio C#Express,并且想知道如何添加一些XML文件并能够在我的代码中引用它们.我将XML文件添加到了项目下的文件夹中,但是不确定如何引用它们,然后将它们复制到输出文件夹中.最初,在我添加它们之前,我只是将XML文件复制到Visual Studio的Debug文件夹中,但是随后当我编译/安装了我编码的程序的新副本时,我不得不手动复制XML文件.

是否可以将XML文件添加到Visual Studio项目中,并能够在代码中引用它们,然后将其复制到输出文件夹中?

解决方案

右键单击项目,添加现有资源,浏览并选择要添加的文件.然后右键单击该文件,然后单击属性,然后将生成操作"更改为内容,将复制到输出目录"更改为复制"(如果较新)(或在需要时始终复制).然后,您可以使用相对路径访问它.

我将其用于XML,并且可以使用以下代码访问我的内容:

XmlDocument document = new XmlDocument();
document.Load("Resources/DefaultConfig.xml");

请注意,我的DefaultConfig.xml文件位于我在Visual Studio中创建的"Resoruces"目录中(这是可选的,但可以帮助我保持项目整洁)

I'm using Visual Studio C# Express and I'm wondering how I would go about adding some XML files and being able to reference them in my code. I added the XML files in a folder under the project, but I'm not sure how I can reference them, and then get them copied to the output folder. Originally, before I added them, I just copied the XML files to the Debug folder for Visual Studio, but then when I compiled/installed a new copy of the program I had coded, I had to manually copy the XML files over.

Is there a way to add XML files to a Visual Studio Project and be able to reference them in the code and then have them copied to the output folder?

解决方案

Right click project, Add Existing Resource, browse and select the file you want to add. Then right click the file and click properties and change "Build Action" to content, and "Copy To Output Directory" to Copy if newer (or copy always if the need be). Then you can access it by using the relative path.

I use this for my XML and I can access my content using the following code:

XmlDocument document = new XmlDocument();
document.Load("Resources/DefaultConfig.xml");

Please note that my DefaultConfig.xml file is inside a "Resoruces" Directory which I created in Visual Studio(this is optional, but it helps me keep my project neat)

这篇关于将XML文件添加到构建中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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