我有以下代码读取超过600个xml文件并从中获取文章信息。基本上每个文件都包含一篇文章的信息,包括标题,大纲,正文等。我的问题是循环并获取/显示文章列表需要30秒以上。
有人可以建议一些优化以下代码的提示吗?
string[] files = System.IO.Directory.GetFiles(ConfigurationManager.AppSettings["FeedLocation"], "*.xml");
IComparer fileComparer = new Helper.CompareFileByDate();
Array.Sort(files, fileComparer);
List<NewsArticle> ListQuery = new List<NewsArticle>();
foreach (string file in files)
{
List<NewsArticle> query = new List<NewsArticle>();
XDocument xmlDoc = XDocument.Load(file);
var query1 = (from feed in xmlDoc.Descendants("NewsML")
select new NewsArticle()
{
AAPTitle = feed.Descendants("HeadLine").FirstOrDefault().Value.ToString(),
AAPSynopsis = (from synopsis in feed.Descendants("Role") where synopsis.Attribute("FormalName").Value == "Abstract" select synopsis.Parent.Element("ContentItem").Element("DataContent").Value).SingleOrDefault().ToString(),
}).ToList();
ListQuery.AddRange(query1);
}
dlAAPArticle.DataSource = ListQuery;
dlAAPArticle.DataBind();