-1

let's say I have a 16x16 GIF image stored in a XML in the exact format below that is part of the C# project as happy_face.xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <file name=“happy_face.gif">
R0lGODlhEAAQAPcAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A/wD//////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMwAAZgAAmQAAzAAA/wAzAAAzMwAzZgAzmQAzzAAz/wBmAABmMwBmZgBmmQBmzABm/wCZAACZMwCZZgCZmQCZzACZ/wDMAADMMwDMZgDMmQDMzADM/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMzADMzMzMzZjMzmTMzzDMz/zNmADNmMzNmZjNmmTNmzDNm/zOZADOZMzOZZjOZmTOZzDOZ/zPMADPMMzPMZjPMmTPMzDPM/zP/ADP/MzP/ZjP/mTP/zDP//2YAAGYAM2YAZmYAmWYAzGYA/2YzAGYzM2YzZmYzmWYzzGYz/2ZmAGZmM2ZmZmZmmWZmzGZm/2aZAGaZM2aZZmaZmWaZzGaZ/2bMAGbMM2bMZmbMmWbMzGbM/2b/AGb/M2b/Zmb/mWb/zGb//5kAAJkAM5kAZpkAmZkAzJkA/5kzAJkzM5kzZpkzmZkzzJkz/5lmAJlmM5lmZplmmZlmzJlm/5mZAJmZM5mZZpmZmZmZzJmZ/5nMAJnMM5nMZpnMmZnMzJnM/5n/AJn/M5n/Zpn/mZn/zJn//8wAAMwAM8wAZswAmcwAzMwA/8wzAMwzM8wzZswzmcwzzMwz/8xmAMxmM8xmZsxmmcxmzMxm/8yZAMyZM8yZZsyZmcyZzMyZ/8zMAMzMM8zMZszMmczMzMzM/8z/AMz/M8z/Zsz/mcz/zMz///8AAP8AM/8AZv8Amf8AzP8A//8zAP8zM/8zZv8zmf8zzP8z//9mAP9mM/9mZv9mmf9mzP9m//+ZAP+ZM/+ZZv+Zmf+ZzP+Z///MAP/MM//MZv/Mmf/MzP/M////AP//M///Zv//mf//zP///yH5BAEAABAALAAAAAAQABAAAAhTAP8JFIiioMGBCAnqW8gQRUKFDCM6RIhiYUWLDQderMhR30WP/z527NjwY8STFk2ilLjRoEuRMF+yZGnwpMOYKieGLGlSJ0SPEh/utCmU4MuEAQEAOw==
</file>
<root>

Trying to figure out how read this resource happy_face.xml from the project's assembly and save it to disk as name="happy_face.gif" in the same place as the program's EXE. Note that the name of the gif file is stored in the XML file to save to disk...

namespace WpfApp1 {
    static class MyXMLTools {

        // Read XML from EXE assembly and 
        // Save XML stored binary file to the local disk
        public static void XmlExtract(
            string NAME = "WpfApp1.happy_face.xml") 
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            
            
            using(Stream stream 
                    = assembly.GetManifestResourceStream(NAME)) {
               using (StreamReader reader
                   = new StreamReader(stream)) {
                  /// ????
                  /// ???
               }
            }
        }

    } // class
} // namespace

Attempt 2:

namespace WpfApp1 {
    static class MyXMLTools {

        // Read XML from EXE assembly and 
        // Save XML stored binary file to the local disk
        public static void XmlExtract(
            string NAME = "WpfApp1.happy_face.xml") 
        {
            Assembly assembly = Assembly.GetExecutingAssembly();            
            Stream stream 
                      = assembly.GetManifestResourceStream(NAME)) {
            XMLDocument mapfile = new XmlDocument();
            mapfile.Load(stream);
            stream.close();

           // ??? ???
        }
Bimo
  • 5,987
  • 2
  • 39
  • 61
  • Read it as string and use this method: https://learn.microsoft.com/cs-cz/dotnet/api/system.convert.frombase64string – Petr Voborník Jul 26 '22 at 18:29
  • Does this answer your question? [C# Convert a Base64 -> byte\[\]](https://stackoverflow.com/questions/6733845/c-sharp-convert-a-base64-byte) – Charlieface Jul 26 '22 at 19:08

1 Answers1

1

This method extract your data to gif binnary:

public static byte[] XmlExtract(string fileName = "WpfApp1.happy_face.xml") 
{
    Assembly assembly = Assembly.GetExecutingAssembly();

    using(Stream stream = assembly.GetManifestResourceStream(fileName))
    {
        string b64 = XDocument.Load(stream).Root.Element("file").Value;
        byte[] gifBinnary = Convert.FromBase64String(b64);
        return gifBinnary;
    }
}

But what do you need to do with them next? Save them to a file and assign it to an image? Or pass them directly to the image like ImageSource?

Petr Voborník
  • 1,249
  • 1
  • 14
  • 11