如何解决创建多个xml文件并将其导出为一个zip文件的最佳方法是什么?
我的项目位于ASP.NET MVC中,现在我正在使用Razor引擎服务(RazorEngineservice.runcompile)创建多个XML文件,并将其作为单个Zip文件导出。
但是问题是,当我们每次传递模型对象以处理模板并将其作为单独的XML文件返回并完成整个操作时,整个内容需要花费更多的时间来完成(10个对象几乎需要40秒)导出。
我当前的方法有什么问题吗?或者我现在是否正确地做到了?如果我在这种方法上有任何错误,请指导我。
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),null,model);
return result;
}
解决方法
您的某些代码丢失了,所以我看不到整个图片,这就是我将从..... gd运气开始的事情。
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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。