Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve an embedded xml resource?

I added an XML file as an embedded resource in my class library by using the accessing the project properties in Visual Studio and then Resources | Add Resource | Add Existing File...

I've tried to access the file using the following code, but I keep getting a null reference returned. Anyone have any ideas?

var path = Server.MapPath("~/bin/MyAssembly.dll");
var assembly = Assembly.LoadFile(path);
var stream = assembly.GetManifestResourceStream("MyNamespace.filename.xml");
like image 728
Kevin Babcock Avatar asked Feb 06 '09 03:02

Kevin Babcock


People also ask

How do you read embedded files?

Method 1: Add existing file, set property to Embedded Resource. Add the file to your project, then set the type to Embedded Resource . NOTE: If you add the file using this method, you can use GetManifestResourceStream to access it (see answer from @dtb).

What does embedded Resource mean?

Embedded resource has no predefined structure: it is just a named blob of bytes. So called “. resx” file is a special kind of embedded resource. It is a string -to-object dictionary that maps a name to an object. The object may be a string , an image, an icon, or a blob of bytes.

How do you add an embedded resource?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.


1 Answers

I find it much easier to use the "Resources" tab of the project's properties dialog in Visual Studio. Then you have a generated strongly typed reference to your resource through:

Properties.Resources.Filename
like image 141
Shawn Miller Avatar answered Sep 24 '22 21:09

Shawn Miller