Click here to Skip to main content
Licence 
First Posted 27 Dec 2004
Views 46,383
Bookmarked 28 times

Working with Embedded Data

By | 10 May 2005 | Article
The article shows you how to load embbeded data (e.g. XML, images) from an assembly.

Introduction

In some cases it is necessary to embed resources. This can be because the user should not be allowed to read or update the files. In this short article, I will show you how you can read from an embedded XML-file which is located within a .NET assembly.

To receive the data located within an XML-file we have to use Reflection. You can find a lot of articles about Reflection at The Code Project or via Google. So I won't explain Reflection in this article.

OK, let's start.

We have an exemplary .NET assembly called Example.dll. This DLL includes some XML-files, one of them is the Test.xml. Now we try to receive the content of Test.xml and give it out via the Console.

First of all we have to load the assembly. This will be done using the following code (assuming that the Example.dll is in the working-directory):

Assembly b = Assembly.LoadFrom(@"Example.dll");

With the following lines of code, you can show the different types that are found within the assembly, but you do not want them to receive the content of Test.xml:

Type[] t = b.GetTypes();
foreach (Type t2 in t) 
{
    Console.WriteLine(t2.FullName);
}

OK, now we will receive the names of all the resources in our assembly:

string[] names = b.GetManifestResourceNames();
foreach(string name in names) 
{
    Console.WriteLine(name);
}

As you know, we need the file Test.xml of our assembly Example.dll. Use the following code to create a stream of data we need:

System.IO.Stream s = b.GetManifestResourceStream("Example.Test.xml");
System.IO.StreamReader sr = new StreamReader(s);
string data = sr.ReadToEnd();
Console.WriteLine(data);

As you can see, the correct data will be shown in the console-window. (The demo application uses list boxes to make the output user-friendly.) Now you are able to load the data into an XmlDocument and manipulate it. Please note that you can't change the resources which are embedded in an assembly (without recompiling) - this means that you can't change resources from your application.

How to load an embedded image

Some of you may ask how to load an embedded image. This isn't more difficult than loading the embedded text:

private void LoadEmbeddedImage() 
{
    Assembly b = Assembly.LoadFrom(@"Example.dll");
            
    System.IO.Stream s = 
       b.GetManifestResourceStream("Example.codeproject.gif");

    System.IO.StreamReader sr = new StreamReader(s);

    Image img = Image.FromStream(s);
    this.picLogo.Image = img;
}

The method LoadEmbeddedImage reads the data from the manifest resource stream and writes the data directly into an image. This image will be shown in our GUI.

How to use the code

  • Start the Example.sln.
  • Compile it first.
  • Now you can start the ExampleTest.

History

  • 12/27/2004
    • Initial post.
  • 12/28/2004
    • Example for loading an embedded image.
    • Updated the GUI for showing some information in a user-friendly way.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Norbert Eder

Team Leader

Austria Austria

Member

Follow on Twitter Follow on Twitter
Norbert Eder currently works as a .NET software architect at UPPER Network. He writes books and articles about WPF, Silverlight and Windows Phone 7.
 
He shares his knowledge using his weblog.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembernbgangsta9:05 31 Mar '11  
GeneralMy vote of 5 Pinmemberjohn_172611:14 26 Nov '10  
GeneralMy vote of 4 PinmemberAdham Ayman11:48 11 Oct '10  
GeneralThanks Pinmembertmdeeper2332:37 21 Jun '08  
GeneralThanks boss PinmemberPadoor Shiras6:27 20 Jun '07  
i tried your code and it works for me. what i want is i have an embedded resource file in the assembly which i have added. now by your code i am able to get the embedded file. in my embedded file i have configsections which i am using for the the current project. so when i am referncing one of the class of the assembly i need to tell this is the resource/app.config file path could you please tell me a solution.
 
if its not clear please get back to me
 

QuestionHow can we write back to embedded XML? Pinmemberxbiplav7:43 26 May '07  
AnswerRe: How can we write back to embedded XML? PinmemberNorbert Eder0:11 28 May '07  
GeneralThis Might Help Pinmemberperlmunger13:09 27 Dec '04  
GeneralRe: This Might Help PinmemberNorbert Eder20:56 27 Dec '04  
GeneralRe: This Might Help PinmemberPerry H12:37 28 Jun '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 10 May 2005
Article Copyright 2004 by Norbert Eder
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid