Monday, July 18, 2005 2:37 PM Christopher

Saving an embedded resource xml file at runtime in C#

I couldn't find a cut & dry solution to this (probably b/c it's too simple for anyone to think they would need an example) so here's how I got an xml file embedded as a resource in a vs2003 project written to the filesystem.

First, you just add an xml file with some default information you want for it to your project and change its build action to Embedded Resource. Then, you find the name of the resource (it can be tricky if you have a few folders) by opening up ildasm and double clicking the MANIFEST node. Using that resource name, you would do something like this:

using System;
using System.IO;
using System.Xml;
using System.Reflection;

string path = Path.Combine(
Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), 
Application.CompanyName);

path = Path.Combine(path, Application.ProductName);
path = Path.Combine(path, subFolder);
path = Path.Combine(path, "fileName.xml");

if(!File.Exists(path)){
	Assembly thisAssembly = Assembly.GetExecutingAssembly();
	Stream rgbxml = thisAssembly.GetManifestResourceStream(
"YourNamespace.fileName.xml");			
	XmlDocument doc = new XmlDocument();
	doc.Load(rgbxml);

	doc.PreserveWhitespace = true;
	doc.Save(path);					
}

A couple of things to note about this: it's for a WinForms application, so I can use the Application class to get things in AssemblyInfo.cs (like ProductName, CompanyName). Also, you could probably do this anywhere but I chose to put a lot of my default configuration under the user's ApplicationData folder, where most users (can't say for sure about the guest account since that's been disabled here for a long time) have authority to write.

Filed under: ,

Comments

# re: Saving an embedded resource xml file at runtime in C#

Monday, July 18, 2005 3:44 PM by stevef

and are the new entries in the xml file available as an embedded resource. Do you not have to recompile the exe with the embedded resource?

# re: Saving an embedded resource xml file at runtime in C#

Monday, July 18, 2005 3:51 PM by Chris Frazier

In this case, I believe that you have to recompile to get a new xml file in there. You may be able to get around it using satellite assemblies if you are worried about having to recompile.

But this is a case that I have a default xml file that has the same content regardless of language, and it remains the same always. This is a good candidate for those cases.

# re: Saving an embedded resource xml file at runtime in C#

Monday, July 18, 2005 4:31 PM by SteveF

ah so like a standard template config file that applies to all new users .....this then gets saved per user and can be edited
Thanks Chris

# re: Saving an embedded resource xml file at runtime in C#

Sunday, July 24, 2005 8:33 AM by Jason Haley

Last year I put together an article that had a bunch of resource files related stuff in it, that people reading this entry might benefit from (some source code there too) at: http://www.jasonhaley.com/articles/resourcefiles/Resource%20Files.htm

# re: Saving an embedded resource xml file at runtime in C#

Monday, February 11, 2008 12:20 PM by Max

It seems worth it to note that "doc.PreserveWhitespace = true;" should be placed before "doc.Load(rgbxml);"

The apparent result, otherwise, is that the embedded file will get loaded, ignoring the whitespace, and then any whitespace left (which will likely be none) will be preserved.

Leave a Comment

(required) 
(required) 
(optional)
(required)