我有一个C#控制台应用程序,可以批量处理XML文件。它从文件夹中抓取批处理,逐个处理每个批处理,然后停止,重新启动和重复。它还检查批处理中的错误,XML验证或丢失文件。如果发生错误,文件夹将移至“错误”文件夹。很简单。
应用程序已经过100个批次或10,000个文件的测试,并且在没有发生错误批次时执行得很好。一般来说,它在98%的时间内运行良好。
有一种情况我还没弄清楚。看起来当发生错误批处理然后在它之后出现一个好的批处理时,好的批处理无法通过XML验证,然后不会将其批处理写入数据库。
跳闸发生在以下代码块中。在此之前,一段时间(di.GetFiles(“*。xml”)。长度> 0)继续处理批次,直到没有剩余。如果没有发生验证错误,则代码进入下面的块。使用XML.Load的try / catch立即看起来很奇怪 - 有时这会失败并且最后一个else语句会执行。更奇怪的是,如果失败的文件被重新提交,他们处理得很好。所以我想知道这里是否有速度问题可能会压倒XDocument或者我应该在这里使用XmlReader,或者已经打开的XDocument会导致这样的事情?
foreach (var file in filesToValidate)
{
var xml = new XDocument();
try
{
xml = XDocument.Load(file);
}
catch (Exception ex)
{
batchErrorsString += string.Format("Batch {0} has an invalid XML file and needs to be resubmitted: {1}<br>", batchNum, file);
}
if (batchErrorsString.Length == 0)
{
var importThing = xml.Descendants("import_thing").Any();
var importThing2 = xml.Descendants("import_thing2").Any();
var Code = "";
var fileDr = "";
var fileRc = "";
if (importThing)
{
Code = xml.Descendants("import_dnr").Select(x => x.Element("code").Value).FirstOrDefault();
fileDr = xml.Descendants("row").Select(x => x.Element("key").Value).FirstOrDefault();
fileRc = xml.Descendants("row").Select(x => x.Element("grp").Value).FirstOrDefault();
}
else
{
Code = xml.Descendants("import_race").Select(x => x.Element("code").Value).FirstOrDefault();
}
var objectType = importDnr ? "THING" : "THING2";
var userName = Environment.UserName;
// create record and populate table
var objectToUpload = _servImp.CreateImportRequestRecord(file, Ids, xml, objectType, Code, userName);
var iid = _repo.InsertTable(objectToUpload);
}
else
{
_servEmail.SendMail(ConfigurationManager.AppSettings["ErrorMailTo"], ConfigurationManager.AppSettings["ErrorMailFrom"], ConfigurationManager.AppSettings["ErrorMailCC"], ConfigurationManager.AppSettings["ErrorMailSubject"], string.Format("Batch {0} is invalid - resubmit", batchNum), "");
}
}
}
任何想法或建议将不胜感激!谢谢!
答案 0 :(得分:0)
Per Pankaj Kapere的评论,您没有清除batchErrorsString
。这意味着一旦遇到错误,后续批次将出错。有两种方法可以解决这个问题。
方法2允许您在完成处理后继续使用batchErrorsString
,而方法1清除每个文件的值。
方法1:
foreach (var file in filesToValidate)
{
var xml = new XDocument();
try
{
xml = XDocument.Load(file);
}
catch (Exception ex)
{
batchErrorsString += string.Format("Batch {0} has an invalid XML file and needs to be resubmitted: {1}<br>", batchNum, file);
}
if (batchErrorsString.Length == 0)
{
//your working code
}
else
{
_servEmail.SendMail(ConfigurationManager.AppSettings["ErrorMailTo"], ConfigurationManager.AppSettings["ErrorMailFrom"], ConfigurationManager.AppSettings["ErrorMailCC"], ConfigurationManager.AppSettings["ErrorMailSubject"], string.Format("Batch {0} is invalid - resubmit", batchNum), "");
batchErrorsString = ""; //Clear here
}
}
方法2:
foreach (var file in filesToValidate)
{
var xml = new XDocument();
try
{
xml = XDocument.Load(file);
var importThing = xml.Descendants("import_thing").Any();
var importThing2 = xml.Descendants("import_thing2").Any();
var Code = "";
var fileDr = "";
var fileRc = "";
if (importThing)
{
Code = xml.Descendants("import_dnr").Select(x => x.Element("code").Value).FirstOrDefault();
fileDr = xml.Descendants("row").Select(x => x.Element("key").Value).FirstOrDefault();
fileRc = xml.Descendants("row").Select(x => x.Element("grp").Value).FirstOrDefault();
}
else
{
Code = xml.Descendants("import_race").Select(x => x.Element("code").Value).FirstOrDefault();
}
var objectType = importDnr ? "THING" : "THING2";
var userName = Environment.UserName;
// create record and populate table
var objectToUpload = _servImp.CreateImportRequestRecord(file, Ids, xml, objectType, Code, userName);
var iid = _repo.InsertTable(objectToUpload);
}
catch (Exception ex)
{
batchErrorsString += string.Format("Batch {0} has an invalid XML file and needs to be resubmitted: {1}<br>", batchNum, file);
_servEmail.SendMail(ConfigurationManager.AppSettings["ErrorMailTo"], ConfigurationManager.AppSettings["ErrorMailFrom"], ConfigurationManager.AppSettings["ErrorMailCC"], ConfigurationManager.AppSettings["ErrorMailSubject"], string.Format("Batch {0} is invalid - resubmit", batchNum), "");
}
}