1

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
}
zeboidlund
  • 9,731
  • 31
  • 118
  • 180

1 Answers1

1

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.

Coincoin
  • 27,880
  • 7
  • 55
  • 76