创建多个 xml 文件并将其导出为一个 zip 文件的最佳方法是什么

What is the best way to create multiple xml files and export it as one zip file

我的项目在 ASP.NET MVC 中,现在我正在使用 Razor 引擎服务 (RazorEngineService.RunCompile) 创建多个 XML 文件并将其作为单个 Zip 文件并导出它。

但问题是,当我们每次传递模型对象来处理模板时,return 它作为单独的 XML 文件并完成整个操作需要更多时间才能完成(几乎~40 秒(10 个对象)导出整个内容。

我目前的做法有什么问题吗?或者我现在做得对吗?如果我在这种方法中有任何错误,请指导我。

private FileInfo Export(List<Model> modelList)
  {
        string timeStr = Datetime.Now.ToString();
        string archiveFileName = "Main.zip";
        string archivePath = Path.Combine(_reportFolderPath, archiveFileName);

        using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
    {
        foreach (var list in modelList)
        {
            string fileName = model.name + model.Id;
            string filePath = GetModelExport(list, fileName, timeStr);
            archive.CreateEntryFromFile(filePath, fileName + ".xml");
        }
        archive.Dispose();
    }
    return new FileInfo(archivePath);
}

private string GetModelExport(Model model, string fileName, string timeStr)
{
    var processedTemplate = ProcessTemplate(model, TemplateName, TemplateKey);
    string reportFilelName = fileName + "_" + timeStr + ".xml";
    string filePath = Path.Combine(_reportFolderPath, reportFilelName);

    using (var file = new StreamWriter(filePath))
    {
        file.Write(processedTemplate);
    }
    return filePath;
}

private string ProcessTemplate(Model model, string templateName, string templateKey)
    {
        var templateFilePath = Path.Combine(_reportTemplateFolder, templateName);
        return ReportUtils.ProcessTemplate(templateFilePath, templateKey, model);
    }
public static string ProcessTemplate(string templatePath, string templateKey, object model = null)
{
    var templateService = RazorEngineService.Create();
    var result = templateService.RunCompile(File.ReadAllText(templatePath), templateKey, null, model);

    return result;
}

你的一些代码丢失了,所以我看不到全貌,这就是我要开始的......祝你好运。


public class HolderTempName 
{
    private TemplateService _templateService;
    private Dictionary<string, string> _templateContainer;
    
    public HolderTempName()
    {
        //this will save creating this everytime
        _templateService = RazorEngineService.Create();
        
        //this will hold the template so it does not have to fetch on each loop,
        //if the same template is used.
        _templateContainer = new Dictionary<string, string>();
    }


    //you will need to tweeek this to get the type out 
    private string GetTemplate(string templateName, templatePath)
    {
        if(!_templateContainer.Conatains(templateName))
        {
            var text = File.ReadAllText(templatePath);
            _templateContainer[templateName]  = text;
        }
        return _templateContainer[templateName];
    }


    private FileInfo Export(List<Model> modelList)
    {
        string timeStr = Datetime.Now;
        string archiveFileName = "Main.zip";
        string archivePath = Path.Combine(_reportFolderPath, archiveFileName);


        using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
        {
            foreach (var item in modelList)
            {
                var templateFilePath = Path.Combine(_reportTemplateFolder, TemplateName); //<--TemplateName seems like a local private
                //these should come from where cant see where...
                var template = GetTemplate( TemplateName, templateFilePath)
                
                string modelResponse  = ProcessModel(item,template,TemplateKey ); //<-- why is not passing in the template
                //step 2; 
                //making this above done in parrell and then add aync, but before all that measure what is taking time
                
                string pathname = MakeFileName(_reportFolderPath, reportFilelName, timeStr);
                SaveToDisk(pathname, modelResponse);
                
                string fileName = model.name + model.Id;
                archive.CreateEntryFromFile(filePath, fileName + ".xml");
            }
            archive.Dispose();
        }
        return new FileInfo(archivePath);
    }

    private string MakeFileName(string path ,string filename, string tStamp)
    {
        string reportFilelName = fileName + "_" + timeStr + ".xml";
        string filePath = Path.Combine(_reportFolderPath, reportFilelName);
        return filePath;
    }

    private void SaveToDisk(string filePath, string content)
    {
        using (var file = new StreamWriter(filePath))
        {
            file.Write(processedTemplate);
        }
    }
    
    public static string ProcessTemplate(object model, string template, templateKey)
    {
        var result = templateService.RunCompile(template, templateKey, null, model);
        return result;
    }
}