部署 XML 文件并加载它
我在解决方案路径中创建了一个 XML 文件。我的解决方案有多个项目(其中一个是服务),现在其中一些项目需要引用 XML 文件
当我尝试使用时
XDocument configXML = new XDocument.Load("Config.xml");
出现错误
找不到符号“加载”
我正在使用 System.Xml.Linq
因为我无法解决问题 1,所以我尝试了
XElement rootElement = XElement.Load("Config.xml");
这个方法,但是当我运行程序时,出现错误:
找不到 xml 文件
由于在 ../Release 文件夹中搜索而
,谁可以将 xml 文件复制到release/debug/deployed 文件夹中?需要一些帮助!
I have created an XML file in my solution path. My solution has multiple projects (one of them is a service), now some of these projects need to refer to the XML file
When I try and use
XDocument configXML = new XDocument.Load("Config.xml");
I get an error
Cannot find symbol 'Load'
I am using System.Xml.Linq
Because I was unable to get around problem 1, i tried
XElement rootElement = XElement.Load("Config.xml");
this worked, but when I run the program, i get an error:
Unable to find the xml file
since it is searching in the ../Release folder, who can I copy the xml file to the release/debug/deployed folders?
Need some assistance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Load 是一个静态方法,您不需要实例:
如果您希望 Config.xml 文件自动复制到输出目录,您可以在解决方案资源管理器中选择它并设置其复制到输出目录属性:
Load is a static method, you don't need an instance:
If you want the Config.xml file to be automatically copied to the output directory you could select it in the solution explorer and set its Copy to Output Directory property:
在解决方案资源管理器中选择该文件,并将该文件的“复制到输出目录”属性设置为“如果较新则复制”或“始终”,并将“生成操作”设置为“无”
Select the file in Solution Explorer, and set the file's "Copy to Output Directory" property to "Copy if newer" or "Always" and Build Action to "None"
正如 Nick 在问题评论中指出的那样,在调用 XDocument.Load 静态方法时不应使用 new 关键字。
因此
应该有效
As Nick has pointed out in comment to the question, you should not use new keyword while calling
XDocument.Load
static method.Hence
should work
每次成功构建项目时,使用构建后事件将文件复制到发布文件夹
http://msdn.microsoft.com/en-us/library/ke5z92ks.aspx
Use Post Build event to copy your file to Release folder each time you successfully built the project
http://msdn.microsoft.com/en-us/library/ke5z92ks.aspx