Xml Tag base64Binary C #

2

I have the following xml tag (I'll summarize it for size):

<docZip schema="procNFe_v3.10.xsd" NSU="000000000001868">H4sIAAAAAAAEAN1aWY+jyLJ+Pv..bla bla bla</docZip>

The field type is base64Binary, it is a Gzip file.

I have already reviewed the internet looking for a way to "read" the XML and get this tag and generate the Gzip file that is contained in it.

How do I get this tag that is in base64Binary and generate the file it contains?

I tried this example code with no success:

using System;
using System.Text;

public class Base64Decoder
{
  public static void Main ()
  {
        string inputText = "This is some text.";
        Console.Out.WriteLine ("Input text: {0}", inputText);
        byte [] bytesToEncode = Encoding.UTF8.GetBytes (inputText);

        string encodedText = Convert.ToBase64String (bytesToEncode);
        Console.Out.WriteLine ("Encoded text: {0}", encodedText);

        byte [] decodedBytes = Convert.FromBase64String (encodedText);
        string decodedText = Encoding.UTF8.GetString (decodedBytes);
        Console.Out.WriteLine ("Decoded text: {0}", decodedText);

        Console.Out.Write ("Press enter to finish.");
        Console.In.ReadLine ();

        return;
  }
}
    
asked by anonymous 17.08.2016 / 15:33

2 answers

1

Hello, I would try doing the following:

byte[] buffer = Convert.FromBase64String("H4sIAAAAAAAEAN1aWY+jyLJ+Pv..bla bla bla");
File.WriteAllBytes(@"c:\home\arquivo.rar", buffer);

I hope I have helped.

Edited response from the message posted below to make it easy for others with the same question.

    
17.08.2016 / 15:42
1

I used the base of this topic to help me, and nothing more just to share a fit, since the content that comes from the tag is an xml so for direct conversion I did the following:

        String base64 = "H4sIAAAAAAAEAIVSXW+CMBT9K4Z3...";

        byte[] buffer = Convert.FromBase64String(base64);
        byte[] xmlret = Decompress(buffer);

        File.WriteAllBytes(@"C:\retorno.xml", xmlret);

The Decompress method:

        static byte[] Decompress(byte[] gzip)
        {
           using (GZipStream stream = new GZipStream(new 
           MemoryStream(gzip),CompressionMode.Decompress))
        {
            const int size = 4096;
            byte[] buffer = new byte[size];
            using (MemoryStream memory = new MemoryStream())
            {
                int count = 0;
                do
                {
                    count = stream.Read(buffer, 0, size);
                    if (count > 0)
                    {
                        memory.Write(buffer, 0, count);
                    }
                }
                while (count > 0);
                return memory.ToArray();
            }
        }
    }

I hope it helps more people!

    
23.03.2018 / 14:51