.NET is a ghetto.
People say the world is getting smaller. That there just isn’t that much left to explore. I suspect the people who say that haven’t coded yet.
The other day, we spent an absurd amount of time trying to find the best way to serialize a simple document for use in an XML Web Service call. Our wish is that this code snippet helps someone else.
Serialize Binary Documents
Here’s how we serialized a binary file to a string based format in C#:
High level, the steps are:
It’s crazy, during research, everyone wanted to talk about serializing POCOs* to send across the wire. EVERYONE. But, that’s only one application of serialization. How about the most simple use case, serializing a simple document.
Well, now you know how.
*As an aside, if you’re not familiar with the term Plain Old C# Object, I encourage you to read up on design principles in leading “Enterprise” Architecture. It’s truly a fascinating read. And yes, I know POCO stands for Plain Old CLR Object, but who are we kidding right? :)
Deserialize Binary Documents
Deserialization - that is converting a Base64String to a binary document - is just as simple. Here’s how:
Where the variable string is a serialized Base64String string. From there you can make tender love to the document. Or whatever else you were planning to do.
‘Till next time…Peace, Love and Happy Coding! :)