title: Decoding base64-encoded data from xml document
body: <p>I receive some xml-files with embedded base64-encoded images, that I need to decode and save as files.</p>
  <p>An unmodified (other than zipped) example of such a file can be downloaded below:</p>
  <p><a href="http://sixfootfour.net/tmp/20091123-125320.zip" rel="noreferrer">20091123-125320.zip</a> (60KB)</p>
  <p>However, I get errors like &quot;Invalid length for a Base-64 char array&quot; and &quot;Invalid character in a Base-64 string&quot;. I marked the line in the code where I get the error in the code.</p>
  <p>A file could look like this:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;windows-1252&quot;?&gt;
  &lt;mediafiles&gt;
      &lt;media media-type=&quot;image&quot;&gt;
        &lt;media-reference mime-type=&quot;image/jpeg&quot;/&gt;
        &lt;media-object encoding=&quot;base64&quot;&gt;&lt;![CDATA[/9j/4AAQ[...snip...]P4Vm9zOR//Z=]]&gt;&lt;/media-object&gt;
        &lt;media.caption&gt;What up&lt;/media.caption&gt;
      &lt;/media&gt;
  &lt;/mediafiles&gt;
  </code></pre>
  <p>And the code to process like this:</p>
  <pre><code>var xd = new XmlDocument();
  xd.Load(filename);
  var nodes = xd.GetElementsByTagName(&quot;media&quot;);
  
  foreach (XmlNode node in nodes)
          {
              var mediaObjectNode = node.SelectSingleNode(&quot;media-object&quot;);
              //The line below is where the errors occur
              byte[] imageBytes = Convert.FromBase64String(mediaObjectNode.InnerText);
              //Do stuff with the bytearray to save the image
          }
  </code></pre>
  <p>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?</p>
  <p>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.</p>
  <p>I have been googling, binging, stackoverflowing and crying - and found no solution... Help!</p>
  <h3>Edit:</h3>
  <p>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...</p>