0

I have an asp.net/C#/Blazor environment, where a button generates an XML with a specific class. With XML Writer, I can make the file, and even can save/download it, but it goes to the server-side (It must to be downloaded on client-side. please, do not argue about it).

I know Blazor implemented some instant download (https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0) and it works perfect with blank/new files, but the problem is, I don't know how to "pass" or convert my previously generated XML with XML Writer method, because Blazor method(s) only allow Stream, char or byte Arrays downloads.

When I tried to convert it, the error

Some of my code is:

    protected async Task CreateXmlFile(int correlativo,string idDocumento, string siglaDocumento, List<DocumentoXML> documentos = null, List<SignersXML> signersXMLs = null,
                                                                                                              List<ContentXMLComplemento> complementos = null,
                                                                                                              List<SignersXMLComplemento> signersComplemento = null)
{
    _xmlWriterSettings = new XmlWriterSettings
        {
            Indent = true,
            Encoding = new UTF8Encoding(false)
        };

    string fullPath= "";
    XmlWriter writer;
    XmlSerializer serializer;

        var documentoIngresoRaiz = new DocumentoIngresoRaiz
            {
                Content_XML = new List<ContentXML>
                {
                    new ContentXML
                    {
                        sve_XML = new List<sveXML>
                        {
                           new sveXML
                           {
                               Documento_XML = documentos
                           }
                        }
                    }
                },
                Signers_XML = signersXMLs
            };

        fullPath = $"{mainPath}Ingreso-{correlativo}.xml";
        var fileName = $"Ingreso-{correlativo}.xml";

        writer = XmlWriter.Create(fullPath, _xmlWriterSettings);
        serializer = new XmlSerializer(typeof(DocumentoIngresoRaiz));
        serializer.Serialize(writer, documentoIngresoRaiz);
        writer.Close(); 

    //I've been trying with these 3 Blazor method lines, to send my xml as stream
    var fileStream = new MemoryStream(writer);
    using var streamRef = new DotNetStreamReference(stream: fileStream);
    await JS.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
}

Error CS1503: Argument 1: cannot convert from 'System.Xml.XmlWriter' to 'byte[]'

I've been looking all around StackOverflow and the Internet with no success. I found some similar posts (I want to download XML file in C# which I created using XmlWriter.Create() on user's system) (How to get a Stream from an XMLWriter?), but they couldn't solve my problem. Any help or tip is welcome. Thank you in advance!

Reza Heidari
  • 1,192
  • 2
  • 18
  • 23
jonhurono
  • 1
  • 1
  • 4
  • Can't you just serialize directly to a `MemoryStream` via `XmlWriter.Create(stream, _xmlWriterSettings);`? – dbc May 23 '22 at 22:50
  • @dbc yeah, I already tried that, but same error appears: "cannot convert from 'System.Xml.XmlWriter' to 'char[]' :/ – jonhurono May 24 '22 at 14:04
  • What line does the compilation error occur on? – dbc May 24 '22 at 14:08

1 Answers1

0

Since there was no way to convert the already generated XML file to byte/stream/char array, I found out that the solution was:

saving this XML file on server-side and then immediately download it to local machine, via JavaScript code (pasted below), passing the fileURL (location of file on the server) and fileName (name of the file)

await JS.InvokeVoidAsync("triggerFileDownload", fileName, fileURL);
    
function triggerFileDownload(fileName, url) {
     const anchorElement = document.createElement('a');
     anchorElement.href = url;
     anchorElement.download = fileName ?? '';
     anchorElement.click();
     anchorElement.remove();
 }
jonhurono
  • 1
  • 1
  • 4