Xml Tag base64Binary C#

Asked

Viewed 1,023 times

2

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

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

The field type is base64Binary, is a gzip file.

I’ve been over the internet looking for a way to "read" the XML and take this tag and generate the Gzip file that’s contained in it.

How do I take this tag that is in base64Binary and generate the file that is contained?

I tested with this example code but failed:

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;
  }
}

2 answers

1


Hi, I’d try to do it this way:

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

I hope I’ve helped.

Reply edited from message posted below to facilitate others with the same doubt.

  • Thanks for the help, but the generated file is empty. However, I managed it with File.Writeallbytes(@"c: home file.rar", buffer); but its answer solved my doubts.

  • 1

    Okay, Robss, it was nothing. I took the liberty of editing my reply so that other people could have access to a functional code, I think it is more "pedagogical". Hug

  • Thanks young....

1

Guys I used the base of this topic to help me, and nothing fairer than sharing a fit, because the content coming 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.