部署 XML 文件并加载它

发布于 2024-12-26 18:23:13 字数 570 浏览 2 评论 0原文

我在解决方案路径中创建了一个 XML 文件。我的解决方案有多个项目(其中一个是服务),现在其中一些项目需要引用 XML 文件

  1. 当我尝试使用时

    XDocument configXML = new XDocument.Load("Config.xml");
    

出现错误

找不到符号“加载”

我正在使用 System.Xml.Linq

  1. 因为我无法解决问题 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

  1. 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

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与酒说心事 2025-01-02 18:23:13

Load 是一个静态方法,您不需要实例:

XDocument configXML = XDocument.Load("Config.xml");

如果您希望 Config.xml 文件自动复制到输出目录,您可以在解决方案资源管理器中选择它并设置其复制到输出目录属性:

在此处输入图像描述

Load is a static method, you don't need an instance:

XDocument configXML = XDocument.Load("Config.xml");

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:

enter image description here

○愚か者の日 2025-01-02 18:23:13

在解决方案资源管理器中选择该文件,并将该文件的“复制到输出目录”属性设置为“如果较新则复制”或“始终”,并将“生成操作”设置为“无”

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"

撕心裂肺的伤痛 2025-01-02 18:23:13

正如 Nick 在问题评论中指出的那样,在调用 XDocument.Load 静态方法时不应使用 new 关键字。

因此

XDocument configXML = XDocument.Load("Config.xml");

应该有效

As Nick has pointed out in comment to the question, you should not use new keyword while calling XDocument.Load static method.

Hence

XDocument configXML = XDocument.Load("Config.xml");

should work

煮茶煮酒煮时光 2025-01-02 18:23:13

每次成功构建项目时,使用构建后事件将文件复制到发布文件夹
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文