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