Skip to content

Instantly share code, notes, and snippets.

@SumindaD
Created January 5, 2020 07:40
Show Gist options
  • Save SumindaD/7a9064fd724bfdad6aa1bf56033ebba9 to your computer and use it in GitHub Desktop.
Save SumindaD/7a9064fd724bfdad6aa1bf56033ebba9 to your computer and use it in GitHub Desktop.
/// <summary>
/// Deserialize the unsigned xml document and retrieve the image
/// </summary>
/// <param name="xmlDocument">Unsigned XmlDocument</param>
/// <returns>Byte[] of the image</returns>
private static byte[] DeserializeXMLToImage(XmlDocument xmlDocument)
{
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
// Save Xml Document to Text Writter.
xmlDocument.WriteTo(xmlTextWriter);
UTF8Encoding encoding = new UTF8Encoding();
// Convert Xml Document To Byte Array.
byte[] xmlDocumentBuffer = encoding.GetBytes(stringWriter.ToString());
XmlSerializer mySerializer = new XmlSerializer(typeof(byte[]));
using (MemoryStream myFileStream = new MemoryStream(xmlDocumentBuffer))
{
return (byte[])mySerializer.Deserialize(myFileStream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment