将 XmlWriter 转换为 Stream / char / byte []

Convert XmlWriter to Stream / char / byte []

我有一个 asp.net/C#/Blazor 环境,其中一个按钮生成具有特定 class 的 XML。使用 XML Writer,我可以制作文件,甚至可以 save/download,但它会转到服务器端(必须 才能在客户端下载-side. 请不要争论它)。

我知道 Blazor 实现了一些即时下载 (https://docs.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0),它与 ​​blank/new 文件完美配合,但问题是,我不知道如何“传递”或转换我以前的文件使用 XML Writer 方法生成 XML,因为 Blazor 方法只允许 Stream、char 或字节数组下载。

当我尝试转换它时,出现错误

我的一些代码是:

    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[]'

我一直在 Whosebug 和 Internet 上四处寻找,但没有成功。 我找到了一些类似的帖子() (How to get a Stream from an XMLWriter?),但它们无法解决我的问题。欢迎任何帮助或提示。提前致谢!

由于无法将已经生成的XML文件转换为byte/stream/char数组,我发现解决方案是:

在 server-side 上保存此 XML 文件 然后立即通过 JavaScript 代码(粘贴在下面)将其下载到本地计算机,传递 fileURL(服务器上文件的位置)和 fileName(文件名)

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