从另一个项目加载资源?

发布于 2024-12-19 17:59:46 字数 1289 浏览 1 评论 0原文

我正在尝试从单独的项目加载 XML 文件;其中一个项目是游戏引擎,它调用 XML 文档读取器并接受指定文件相对目录的路径。

来自引擎

XDocument doc;

            try
            {
                Stream stream = this.GetType().Assembly.GetManifestResourceStream(path);

                doc = XDocument.Load(stream);
            }
            catch
            {
                doc = XDocument.Load(path);
            }

来自其他项目

string filePath = "Test.xml";

Npc npc = new Npc("somename", 2,filePath);

Test.xml 驻留在其他项目的根目录中。 Npc 构造函数调用 Statistics 对象构造函数,然后调用加载 XDocument 的方法。当发生这种情况时,filePath 只是向下传递到各层。

我看过这个,并尝试了嵌入式资源示例,这最终是我想要完成的目标,但它对我不起作用。

我在这里做错了什么?

更新

我将 Text.xml 更改为 Chronos.Text.xml,因为这是文件所在的位置。在我的调试器中,我看到当我将其用作路径时,流仅返回 null:

try
{
    Stream stream = this.GetType().Assembly.GetManifestResourceStream("Chronos.Test.xml"); //returns null

    doc = XDocument.Load(stream); //Exception thrown
}
catch
{
    doc = XDocument.Load(path); //File not found
}

I'm trying to load an XML file from a separate project; One of these projects is a game engine, which calls the XML document reader and takes in a path specifying the relative directory to the file.

From Engine

XDocument doc;

            try
            {
                Stream stream = this.GetType().Assembly.GetManifestResourceStream(path);

                doc = XDocument.Load(stream);
            }
            catch
            {
                doc = XDocument.Load(path);
            }

From the other project

string filePath = "Test.xml";

Npc npc = new Npc("somename", 2,filePath);

Test.xml resides in the other project's root directory. The Npc constructor makes a call to a Statistics object constructor, which then calls the method which loads the XDocument. As this is happening, the filePath is simply passed downward through the layers.

I've looked at this, and tried the embedded resource example, which is ultimately what I'm trying to accomplish, and it didn't work for me.

What am I doing wrong, here?

Update

I changed Text.xml to Chronos.Text.xml, as that is where the file resides. In my debugger, I see that the stream simply returns null when I use that as a path:

try
{
    Stream stream = this.GetType().Assembly.GetManifestResourceStream("Chronos.Test.xml"); //returns null

    doc = XDocument.Load(stream); //Exception thrown
}
catch
{
    doc = XDocument.Load(path); //File not found
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

长途伴 2024-12-26 17:59:46

嵌入资源直接嵌入到可执行文件中。 Assembly.GetManifestResourceStream() 正在尝试打开嵌入资源上的流,您应该提供以下格式的资源名称 AssemblyDefaultNamespace.Directory.Filename

如果您尝试从另一个目录打开 XML 文件,则必须提供 XDocument.Load()完整路径,或当前项目的相对路径输出目录指向另一个目录。

另一种解决方案是将数据从其他项目复制到您的项目中,并指定您希望将文件复制到输出目录。

Embeded resources are embeded directly in the executable. Assembly.GetManifestResourceStream() is trying to open a stream on the embeded resource, what you should be providing is the resource name in the following format AssemblyDefaultNamespace.Directory.Filename.

If you are trying to open an XML file from another directory, you will have to provide the full path to XDocument.Load(), or a path relative from your current project output directory pointing to that other directory.

Another solution would be to copy the data from the other project into your project and specify that you want the file to be copied to your output directory.

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