Search code examples
c#xmlresourcemanager

How to write data in xml file that is embedded in a dll


I have a user control in that I embedded an xml file to store emails. Now I compiled it and made a single dll. Now I'm using this user control in a windows form and I need to write the data in that embedded xml file on a click of the button. I easily can access the data from embedded xml file by using

var doc = XDocument.Parse(Resource.EmailHistory);
 var email=doc.Root.Elements().Select(x => x.Element("Email"));
 foreach (string item in email)
 {
      textBox1.Text+= item;
 }

but facing the problem while writing into the embedded xml file. please help me with this. Any help would be appreciated.

Xml Code

<?xml version="1.0" encoding="utf-8" ?>
<root>
   <Email>
       Hello
   </Email>
   <Email>
       There
   </Email>
</root>

Solution

  • You can't write the data into an embedded XML file, because it's a resource embedded in the DLL (or an executable).

    You can potentially replace it by creating an entire new resource, and then using UpdateResource. This isn't as simple as it sounds; you have to do all of the following (see Updating Resources at MSDN for details):

    1. Use LoadLibrary to load the executable file Hand.exe.
    2. Use FindResource and LoadResource to locate and load the dialog box resource.
    3. Use LockResource to retrieve a pointer to the dialog box resource data.
    4. Use BeginUpdateResource to open an update handle to Foot.exe.
    5. Use UpdateResource to copy the dialog box resource from Hand.exe to Foot.exe.
    6. Use EndUpdateResource to complete the update.

    Having to do this much work just to update an XML file should tell you that you're going about it the wrong way, and that an embedded resource isn't the proper approach. (An external file or a database (which would allow a lot more information to be stored, and could be sorted, filtered, and searched) would be a much better solution, for instance.)