Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 472953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:08:17+00:00 2026-05-13T00:08:17+00:00

I receive some xml-files with embedded base64-encoded images, that I need to decode and

  • 0

I receive some xml-files with embedded base64-encoded images, that I need to decode and save as files.

An unmodified (other than zipped) example of such a file can be downloaded below:

20091123-125320.zip (60KB)

However, I get errors like "Invalid length for a Base-64 char array" and "Invalid character in a Base-64 string". I marked the line in the code where I get the error in the code.

A file could look like this:

<?xml version="1.0" encoding="windows-1252"?>
<mediafiles>
    <media media-type="image">
      <media-reference mime-type="image/jpeg"/>
      <media-object encoding="base64"><![CDATA[/9j/4AAQ[...snip...]P4Vm9zOR//Z=]]></media-object>
      <media.caption>What up</media.caption>
    </media>
</mediafiles>

And the code to process like this:

var xd = new XmlDocument();
xd.Load(filename);
var nodes = xd.GetElementsByTagName("media");

foreach (XmlNode node in nodes)
        {
            var mediaObjectNode = node.SelectSingleNode("media-object");
            //The line below is where the errors occur
            byte[] imageBytes = Convert.FromBase64String(mediaObjectNode.InnerText);
            //Do stuff with the bytearray to save the image
        }

The xml-data is from an enterprise newspaper system, so I am pretty sure the files are ok – and there must be something in the way I process them, that is just wrong. Maybe a problem with the encoding?

I have tried writing out the contents of mediaObjectNode.InnerText, and it is the base64 encoded data – so the navigating the xml-doc is not the issue.

I have been googling, binging, stackoverflowing and crying – and found no solution… Help!

Edit:

Added an actual example file (and a bounty). PLease note the downloadable file is in a bit different schema, since I simplified it in the above example, removing irrelevant stuff…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:08:18+00:00Added an answer on May 13, 2026 at 12:08 am

    For a first shot i didn’t use any programming language, just Notepad++

    I opened the xml file within and copy and pasted the raw base64 content into a new file (without square brackets).

    Afterwards I selected everything (Strg-A) and used the option Extensions – Mime Tools – Base64 decode. This threw an error about the wrong text length (must be mod 4). So i just added two equal signs (‘=’) as placeholder at the end to get the correct length.

    Another retry and it decoded successfully into ‘something’. Just save the file as .jpg and it opens like a charm in any picture viewer.

    So i would say, there IS something wrong with the data you’ll get. They just don’t have the right numbers of equal signs at the end to fill up to a number of signs which can be break into packets of 4.

    The ‘easy’ way would be to add the equal sign till the decoding doesn’t throw an error. The better way would be to count the number of characters (minus CR/LFs!) and add the needed ones in one step.

    Further investigations

    After some coding and reading of the convert function, the problem is a wrong attaching of a equal sign from the producer. Notepad++ has no problem with tons of equal signs, but the Convert function from MS only works with zero, one or two signs. So if you fill up the already existing one with additional equal signs you get an error too! To get this damn thing to work, you have to cut off all existing signs, calculate how much are needed and add them again.

    Just for the bounty, here is my code (not absolute perfect, but enough for a good starting point): 😉

        static void Main(string[] args)
        {
            var elements = XElement
                .Load("test.xml")
                .XPathSelectElements("//media/media-object[@encoding='base64']");
            foreach (XElement element in elements)
            {
                var image = AnotherDecode64(element.Value);
            }
        }
    
        static byte[] AnotherDecode64(string base64Decoded)
        {
            string temp = base64Decoded.TrimEnd('=');
            int asciiChars = temp.Length - temp.Count(c => Char.IsWhiteSpace(c));
            switch (asciiChars % 4)
            {
                case 1:
                    //This would always produce an exception!!
                    //Regardless what (or what not) you attach to your string!
                    //Better would be some kind of throw new Exception()
                    return new byte[0];
                case 0:
                    asciiChars = 0;
                    break;
                case 2:
                    asciiChars = 2;
                    break;
                case 3:
                    asciiChars = 1;
                    break;
            }
            temp += new String('=', asciiChars);
    
            return Convert.FromBase64String(temp);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I received some xml data that includes a timestamp taken at midnight of the
We are running some sites that receive decent traffic and occasional leechers that hog
I am having some issues receiving UTF-8 XML files back from DHL API. As
I receive an XML file with encoding ISO-8859-1 (Latin-1) Within the file (among other
I'm trying to build a web-service, which will receive large files and save them
I have some pretty big xml files which used for scheduled import. I use
I have some raw data (xml) which I definitely receive containing unicode. I write
I need to create a PHP script that receives XML input via an HTTP
We have pretty big (~200mb) xml files from different sources that we want to
Having problems deserializing some xml into an object in C#. The error that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.