Reading embedded files

Hi, i've added a .xml file to my xna project for windows, i've set it to be an embedded resource, how i can i them read the file n e attempt i make seems to try to read a file from the hdd or from a url

many thanks



Answer this question

Reading embedded files

  • Neal Sidhwaney

    I think you can do this using Assembly.GetExecutingAssembly().GetFile(), but am not 100% sure about that.


  • David Mulford

    If you are planning to embed more than one level per assembly, I strongly suggest you just deploy the XML files alongside instead. The NetCF runtime loads the entire assembly into memory when it is loaded, so having all your levels embedded means you will load all the levels at the beginning of the game, whether you use all those resources or not.

    Since assemblies cannot be unloaded very easily, I would suggest putting your data files alongside the assembly so that you can load them when you need them (for example, one file per level).

    If your levels are very small, then this won't be a concern. I just want to point this out before it becomes a problem for you.

    --Stephen


  • rako77

    You could try looking at your assembly using .NET Reflector to make sure you have the right resource name.


  • OniShiro

    Stephen suggested you need to use GetManifestResourceStream instead of GetFile.


  • Jerod Moemeka

    Shawn Hargreaves - MSFT wrote:
    You could try looking at your assembly using .NET Reflector to make sure you have the right resource name.

    as in http://www.aisto.com/roeder/dotnet/ is so i open the exe and just get loads of Object reference not set to an instance of an object and see no ref to the .xml file


  • SnakeSV

    many thanks for all your help, i would intend on trying on a 360 sometime in the near future so this is great
  • Keithyboy1

    Ah, I see where the confusion is coming from.

    .resx files are for design-time only. They are used as input by the resource compiler (resgen ) and then the resulting .resources file is embedded into the assembly by the compiler (csc) using a /resource switch. That embeds the .resources file into the assembly, and a ResourceManager must be used to retrieve resources from that embedded file.

    In the IDE, there are two ways to embed files into your assembly. You can either embed files by using the resource designer, or you can add the file to your project and edit the Build Action property.

    Embedding a file using the resources designer requires the use of a ResourceManager to retrieve it.

    Using the Embedded Resource build action passes the file directly to the C# compiler using a /resource switch. In that case, you can get the file from the assembly using Assembly.GetFile.

    I did not see any mention of how it was embedded, and I assumed the use of the resource designer. I didn't realize exactly what the Embedded Resource action did until now. Either way will work, it just depends on how the file was embedded in the first place.

    --Stephen

    [Edit: I do see now that it was "set" to be an embedded resource. Sorry for my lack of attention.]


  • bora.erbas

    SOrry, only tried getfile as the first method gives exact same error
  • gzcjun

    ok the following line of code

    XmlTextReader reader = new XmlTextReader(Assembly.GetExecutingAssembly().GetFile("propladderwin.codehelp.xml"));

    while (reader.Read())

    {

    }

    throws a n erro Object reference not set to an instance of an object. when compiled and run n e idea what may cause this


  • Bahtiyar Omarov

    Sorted, just needed to remove the .xml from the file name and it works, thanks to all, this will make embedding level info much easier
  • Will Merydith

    You need to use the System.Resources.ResourceManager class.

    For example,

    System.Resources.ResourceManager temp = System.Resources.ResourceManager("MyGame1.Properties.Resources", typeof(Resources).Assembly);

    object obj = ResourceManager.GetObject("foo", resourceCulture);

    byte[] myFileCalledFoo = ((byte[])(obj));

    Since you embedded an XML file, it was probably embedded as a string rather than an unknown file (which is byte[]). Your code must also vary from this example depending on the embedded resource names, by the way.

    --Stephen


  • Suby786

    Hmm, actually I just tried the Assembly.GetFile approach and couldn't get it to return the embedded data.

    What you want to do instead is this:

    Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication1.settings.xml");

    In this example, WindowsApplication1 is the root namespace, and "settings.xml" is the file I embedded. The resource name is a combination of those.

    --Stephen


  • SQLirrel

    String resources are the only embedded types supported on the 360, I believe. So the above would be a good idea if you plan on using your code there.
  • marelis

    I thought the ResourceManager was for reading from .resx files

    That's a different kind of embedding to if you select "Embedded Resource" as the Build Action in the VS properties.


  • Reading embedded files