Click here to Skip to main content
15,851,090 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm parsing an XML file and its values may or may not be Base64-encoded.

I'm reading an XML file with XmlReader and then writing it back out to an output file (after I've fiddled with the values of some of the elements).
If the input file was Base64-encoded, the output file needs to be as well.

How do I

a) tell if the values are Base64 encoded or not in the first place?

And if so...

How do I

b) Read the encoded value and change it into a plaintext value during parsing using an XmlReader class?

c) Write the value back out to the output either with Base64 encoding or plaintext, depending on whether the input file was the same.

If you could point me to a class or method that would help out that would be great.

Thanks.
Posted
Updated 18-May-11 11:17am
v3
Comments
Dalek Dave 18-May-11 16:06pm    
Edited for Grammar and Readability.

It is theoretically impossible to tell if the data is Base64 or not. This format does not have any indicator of the format, and for a good reason. The whole idea of format detection is wrong and cannot be strictly implemented. In all cases, you need some meta-data.

For base64 conversions use the class System.Convert, see http://msdn.microsoft.com/en-us/library/system.convert.aspx[^]. You can use its methods: <code>FromBase64CharArray, FromBase64String, ToBase64CharArray, ToBase64String.

—SA
 
Share this answer
 
v2
Comments
Brian C Hart 18-May-11 16:16pm    
Actually there is no theoretical about it...I've been told by the client that either the values will be in Base64 or they won't. So technically all I really need to do is an Int32.Parse and if it throws a FormatException then I "know" the value is really in Base64.
Sergey Alexandrovich Kryukov 18-May-11 16:32pm    
"Actually there is no theoretical about it" -- this phrase makes no sense, in connection to my statement. You cannot detect it. It's the design of the data -- it should contain meta-data portion which indicates what kind of data is that. For example, different formats can be supplied in different elements; and unused element can be missing (or empty).

Thanks for accepting this answer.
Good luck,
--SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-May-11 16:02pm    
1) pretty bad links, 2) no matter what is written there detection of format is theoretically impossible; only the educated guess can be used.
--SA
Brian C Hart 18-May-11 16:17pm    
See my comment on the below post. For this particular project, I will know a priori if it's Base64. I've been told by the client the file is guaranteed to either be in Base64 or plaintext. I just needed to brainstorm about what the best method was to automate detecting which I have is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900