Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding from Base64 in C#

I created XML Document and saved this document as

  XmlDocument xmlDoc = new XmlDocument();
  XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
  xmlDoc.AppendChild(dec);
  XmlTextWriter writer = new XmlTextWriter(fullPath,Encoding.UTF8);
  writer.Formatting = Formatting.Indented;
  xMLDoc.Save(writer);
  writer.Flush();

And then I have encoded this document using Base64 Encoder

The Decoder could not parse XML file. I created the decoder myself and got this result

 ?<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<ClinicalDocument 
 xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
classCode=\"DOCCLIN\" moodCode=\"EVN\" schemaLocation=\"urn:hl7-org:v3
CDA.xsd\">\r\n  <typeId extension=\"POCD_HD000040\" root=\"2.16.840.1.113883.1.3
\" />\r\n

Please help me to resolve the issue. How I have to save the XML file to avoid the issues? Or How I have to encode to Base 64 to resolve the issue? I am using base64 encoder to encode xml file. I am requesting document. it is required to use base64 encoder. I decoded myself to check where is the problem. The decoder is Java . They can not parse the xml file I believe because ?< in front of the document.

like image 253
Rose Avatar asked Sep 09 '11 22:09

Rose


People also ask

How do I decode a Base64 string?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.

What is Base64 encode and decode?

Base64 is an encoding and decoding technique used to convert binary data to an American Standard for Information Interchange (ASCII) text format, and vice versa.

What is Base64 algorithm?

A. Base64 Algorithm The Base64 algorithm is one of the algorithms for Encoding and Decoding an object into ASCII format, which is meant for the base number 64 or one of the methods used to encode the binary data[11], [12].


1 Answers

It depends on how you're encoding it, but you should use UTF-8 since the document is declared as such. Here are examples for encoding and decoding:

See Jon Skeet's answer here:
C# base64 encoding/decoding with serialization of objects issue

To encode:

public string EncodeStringToBase64(string stringToEncode)
{
    return Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToEncode));
}

To decode:

public string DecodeStringFromBase64(string stringToDecode)
{       
    return Encoding.UTF8.GetString(Convert.FromBase64String(stringToDecode));
}
like image 124
James Johnson Avatar answered Sep 22 '22 02:09

James Johnson