Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
Programming Blogs - Blog Catalog Blog Directory
 
 
 

Links

Social

Reading embedded files at runtime

I have class library that contains some XML-files as embedded resources. Reading these files at runtime is very easy. It takes only couple of lines of code.

Let’s assume we have class library MyExamples.TestLibrary where we have XML-file called mappings.xml. When we compile our class library then mappings.xml will be put inside assembly as embedded resource. Here is the code to read this XML-file (you need to import System.IO, System.Reflection and System.XML namespaces).

C#
var fileName = "MyExamples.TestLibrary.mappings.xml";
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(fileName);
 
if (stream == null)
{
    throw new FileNotFoundException("Cannot find mappings file.",
fileName);
}
 
var doc = new XmlDocument();
doc.Load(stream);
 
// Do something cool with embedded XML

VB.NET


Dim fileName = "MyExamples.TestLibrary.mappings.xml"
Dim assembly = Assembly.GetExecutingAssembly()
Dim stream = assembly.GetManifestResourceStream(fileName)

If stream Is Nothing Then
    Throw New FileNotFoundException("Cannot find mappings file.", _
        fileName)
End If

Dim doc = New XmlDocument()
doc.Load(stream)

' Do something cool with embedded XML

Some things to notice. If there is no resource we are asking then stream will be null. No exception will be thrown. Second thing is resource name. We don’t use only file name but also assembly name to locate the resource. If you don’t provide assembly name then resource is not found and resource stream is null.


kick it on DotNetKicks.com pimp it Shout it
Posted: May 30 2009, 11:59 AM by DigiMortal | with 9 comment(s)
Filed under:

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# May 30, 2009 5:18 AM

DotNetBurner - .net Framework said:

DotNetBurner - burning hot .net content

# May 30, 2009 5:20 AM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# May 30, 2009 5:22 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 30, 2009 5:25 AM

IT Questions » Blog Archive » Reading embedded files at runtime said:

Pingback from  IT Questions  » Blog Archive   » Reading embedded files at runtime

# May 31, 2009 6:18 AM

ComponentGear.com Feed said:

This week on Channel 9, Dan and Brian cover the week's top developer news including: - Microsoft

# June 6, 2009 12:38 PM

This Week on C9: Project Natal, Qex for SQL, and Bing for Devs | Tech-monkey.info Blogs said:

Pingback from  This Week on C9: Project Natal, Qex for SQL, and Bing for Devs | Tech-monkey.info Blogs

# June 6, 2009 12:45 PM

Donald said:

My favorite one liner to get a schema from an embedded resource:

this.task.ReadXmlSchema(new XmlTextReader(new StringReader(Resources.Task)));

# June 7, 2009 8:59 PM

This Week on C9: Project Natal, Qex for SQL, and Bing for Devs said:

Pingback from  This Week on C9: Project Natal, Qex for SQL, and Bing for Devs

# June 9, 2009 2:44 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)