Convert a .pdf or .p7s file to base64 in C #

2

I want to convert a .pdf or .P7s file to base64 in C # which should be loaded into XML. How can I do it?

EX:

<conteudoArquivoP7S>
    MIMCmfgGCSqGSIb3DQEHAqCDApnoMIMCmeMCAQExCzAJBgUrDgMCGgUAMIMCexwGCSqGSIb3DQEHAaCDAnsMBIMCewclUERGLTEuMgolZGhpOWhrbGZycDI1CqB5AgAEAAAACwAAAAZ7AgAKMSAwIG9iago8PAovUHJvZHVjZXIgKCkKPj4KZW5kb2JqCjIgMCBvYmoKPDwKL1R5cGUgL0NhdGFsb2cKL1BhZ2VzIDMgMCBSCj4+CmVuZG9iago0IDAgb2JqCjw8Ci9UeXBlIC9QYWdlCi9NZWRpYUJveCBbIDAgMCA2MTIgODQzIF0KL1BhcmVudCAzIDAgUgovUmVzb3VyY2VzIDw8Ci9YT2JqZWN0IDw8Ci9JbTEgNyAwIFIKPj4KL1Byb2NTZXQgWyAvUERGIC9JbWFnZUMgXQo+PgovQ29udGVudHMgNSAwIFIKPj4KZW5kb2JqCjUgMCBvYmoKPDwKL0xlbmd0aCA2IDAgUgo+PgpzdHJlYW0KcQo2MTIgMCAwIDg0MyAwIDAgY20KL0ltMSBEbwpRCmVuZHN0cmVhbQplbmRvYmoKNiAwIG9iagozMAplbmRvYmoKOSAwIG9iago8PAovTGVuZ3RoIDEwIDAgUgoKPj4Kc3RyZWFtCkEKZW5kc3RyZWFtCmVuZG9iagoxMCAwIG9iagoxCmVuZG9iago3IDAgb2JqCjw8Ci9UeXBlIC9YT2JqZWN0Ci9TdWJ0eXBlIC9JbWFnZQovTmFtZSAvSW0xCi9XaWR0aCA4NTAKL0hlaWdodCAxMTcwCi9CaXRzUGVyQ29tcG9uZW50IDgKL0xlbmd0aCA4IDAgUgovQ29sb3JTcGFjZSAvRGV2aWNlUkdCCi9GaWx0ZXIgWyAgL0RDVERlY29kZSBdCj4+CnN0cmVhbQr/2P/gABBKRklGAAEAAQAAAAAAAP/+AB9MRUFEIFRlY2hub2xvZ2llcyBJbmMuIFYxLjAxAP/bAIQAEhISHRQdLRsbLS0gICAtMSsrK
</conteudoArquivoP7S>
    
asked by anonymous 12.05.2015 / 16:54

1 answer

3

A generic base64 file encoding code should resolve:

using (var fs = new FileStream("C:\Caminho\Do\Arquivo.pdf", FileMode.Open, FileAccess.Read)) 
{
    byte[] bytesArquivo = new byte[fs.Length];
    fs.Read(bytesArquivo, 0, Convert.ToInt32(fs.Length));
    string stringEmBase64 = Convert.ToBase64String(bytesArquivo); 
}

To make XML, use Mark Rasmussen's XmlOutput class . I made a NuGet package for her . The usage is like this:

var xo = new XmlOutput()
    .XmlDeclaration()
    .Node("conteudoArquivoP7S").InnerText(stringEmBase64);
    
12.05.2015 / 17:08