Nicko think on software, web, ideas

October 17, 2007

Get XML from Embedded Resources

Filed under: Code and Code — Nicko Satria Utama @ 9:43 am

For some situation, I need to save data or configuration as a default to the DLL and EXE files. First I make XML file in Visual Studio and fill the contents. After that set XML file’s Build Action to Embedded Resource. After compiling and produce DLL or EXE you can access the resources using the methods below.

/// <summary>
/// Get XML from Embedded Resources
/// </summary>
/// <returns></returns>
public XmlReader GetXmlConfig()
{

XmlReader xmlReader = null;
Assembly asm = Assembly.LoadFrom(“myLibrary.dll”);
if (asm != null)
{

string[] strArr = asm.GetManifestResourceNames();
if (strArr != null && strArr.Length > 0)

{

foreach (string str in strArr)
{

if (str.EndsWith(“MyXML.xml”))
{

Stream stream = asm.GetManifestResourceStream(str);
xmlReader = XmlReader.Create(stream);

}

}

}

} return xmlReader;

}

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.