Embedded XML

  • Thread starter Thread starter Chuck Bowling
  • Start date Start date
C

Chuck Bowling

I have a user control designed to edit the records in a small XML typed
dataset. I want to keep the control and the dataset together when the
control is dropped onto a form. The problem is that when I drop the control,
the system looks for the *.xml file in the Visual Studio installation
directory.

I'm guessing that I need to embed the xml into the control dll. If that's
the case, how do I do that? And, would the dataset be a *.resx file or a
regular *.xml?
 
You can probably add it as a resource - but in my understanding you'd
have to do this every time you rebuilt the dll!

to add as a resource, file>open open your dll - then click on resources
and add a new one - add the xml file.

Not 100% sure it works exactly like that - but that's what i remember
from doing it a few weeks ago.
 
Just add the xml file to your project and make it an embedded resource file.
Now using refection you can read the file from the assembly manifest:
GetType().Module.Assembly.GetManifestResourceStream("namespace.file.xml")
Note the namespace is required. Also this is a read only copy. If you want
read/write, you could work with it in memory and save to a file or iso
storage, etc.
 
Not that I know of. The stream per below is readonly. Even if you could,
you would be changing your exe or dll all the time and invalidating your
hash of the assembly so strong name would not work unless it was regenerated
each time. I think that would get pretty messy. If you need default data,
then load from manifest using xmlserializer after you get the string, and
write to file or iso storage and read/write as needed. For that matter, if
you use a class to represent your xml, you don't need the manifest at all.
You could just create a static method that returns an instance of your class
with specific defaults and then read/write to the class until you need to
serialize to persistent storage or something. What is the need so we can
help better?

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
thisExe.GetManifestResourceStream("MyNamespace.MyFile.xml");
string xml = null;
using(StreamReader sr = new StreamReader(file))
{
xml = sr.ReadToEnd();
}
Console.WriteLine(xml);
Console.WriteLine("Can write:"+file.CanWrite);
 
Back
Top