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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 253k
  • Answers 253k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You'll need to keep track of the changes client side,… May 13, 2026 at 9:51 am
  • Editorial Team
    Editorial Team added an answer Typically an array or list includes entries, indexed by an… May 13, 2026 at 9:51 am
  • Editorial Team
    Editorial Team added an answer After you construct each $url, use file_get_contents($url) May 13, 2026 at 9:51 am

Related Questions

I have a file, which is in XML format (consists just of root start
There have been many debates about this topic already here, but none of them
Ok, I've run across my first StackOverflowError since joining this site, I figured this
WSS 3.0 List Service I am running GetListItems() on a Picture Library (name Pictures)

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.