views:

33

answers:

4

I am programming in .NET (C#) using Visual Studio. I want to use am XML file to store some information in my program. No other programs will ever need to access this XML file and no one will need to access/modify it manually our without using the program. I am wondering what build options I should select for this file or if it really makes a difference? Should it be an embedded resource or content file, etc? If i could build it right in to the .exe I would but I don't think that is possible. Also, if I have to do anything special to reference the file please comment on that.

A: 

This technique is described in detail at the following address:

http://www.devx.com/dotnet/Article/10831/1954

Philip Smith
-1 GetManifestResourceString
Will
A: 

You can add your xml file as an embedded resource and then load it through Assembly.GetManifestResourceStream method

var pathToAssembly = "c:\\assembly.dll";
var assembly = Assembly.LoadForm(pathToAssembly);
var pathToResourceWithinAssembly = "assembly.YourEmbeddedXML.xml"; //assembly is the name of your assembly where embedded resource defined
var stream = assembly.GetManifestResourceStream(pathToResourceWithinAssembly);
var ebeddedXML = string.Empty;
if (stream != null)
            {
                var streamReader = new StreamReader(stream);
                ebeddedXML = streamReader.ReadToEnd();
            }
Eugene Cheverda
Again with the GMRS. This is OOOOLLLLDDD school. Add a file as a resource in Visual Studio and it generates a strongly-typed property of the static Resources class (MyBaseNamespace.Properties.Resources).
Will
If you will need to use xmls in an assembly that can't have a reference to another assembly where resource is declared (for example, circular dependencies problem), what will you do then? ;)
Eugene Cheverda
@Eugene actually, you can do one of two things. First, you can make it a public resource, and second you can use the InternalsVisibleTo attribute.
Will
@Will, I mean that, for example, you have created two projects: Common and Core. Your resource declared in Core, Core has a reference to Common, BUT you need to use the same resource of Core IN Common, which can't have a reference to Core to use its Core.Properties.Resources.MyXML. The approach with Resources is good enaugh to some cases and it does not apply to the other cases. Am I right? So I don't understand why you're minusing our answers with using GMRS.
Eugene Cheverda
@eugene IMHO your scenario is one out of a thousand. Besides, your scenario would dictate that shared resources be placed in Common, as would any shared classes. Also, your proposed scenario does not apply to anything the asker included in his question. Can you, for any given practice, find an example where it would be required? Sure. Does that mean your practice is best? Not at all. And, in the case of 99.9% of people who need to consume a resource, your answer is not best. That's why I downvote it. If I'm wrong, others will counteract my vote. Nothing personal.
Will
+1  A: 

Set the Build Action to be Embedded Resource, then use code something like this to load it into an XML Document object...

Dim objResource As New Xml.XmlDocument

objResource.Load(Assembly.GetExecutingAssembly.GetManifestResourceStream("Resource.xml"))

UPDATE See Will's correct comments below, in which case the code (in VB.Net) would be this instead...

Dim objResource As New Xml.XmlDocument

objResource.LoadXml(My.Resources.ResourceName)
barrylloyd
or just use Properties.Resources.MyStronglyTypedResource
Will
Aaaah, much better!
Will
+1  A: 

Add your file to the project as a resource. See this MSDN article for how to do this.

By using this method, the resource is added to your assembly and is exposed as a strongly-typed property of the Resources class (BaseNamespace.Properties.Resources). It is much easier to use than the old school GetManifestResourceStream version, plus you get compile time safety.

Will
If nobody noticed GMRS bugs the hell outta me. When I was starting out all I could find on how to do this was GMRS, which caused me lots of trouble to get working reliably and would result in bugs as things were changed during development. I was so glad and mad when I found out that Visual Studio has a much better alternative...
Will
@Will: I noticed ;)
barrylloyd
I want to use xmlDataDocument.xmlRead(Properties.Resources.xyz) to load the data into a dataset of the xmldatadocument. How is this possible?
Jordan S
@Jordan the xml will be a text file; the property will be of type `System.String`. You just need to use an XDocument or XmlDocument method that loads xml from a string (see barrylloyd's answer for an example). Dunno wat an xmlDataDocument is, so can't answer that.
Will