2
今天早上我真的需要去別的地方。所以,我決定在這裏發佈一個性能問題。你能改善這個linq-to-xml方法的性能嗎?
下面的代碼工作,但它多次調用Load和Save方法。這似乎遠沒有效率。請有人提供代碼到目前爲止加載和保存線出現在循環之外。我想調用加載並保存一次。
感謝傢伙:)
public void RemoveNodes(IList<String> removeItems)
{
foreach (String removeItem in removeItems)
{
XDocument document = XDocument.Load(fullFilePath);
var results = from item in document.Descendants(elementName)
let attr = item.Attribute(attributeName)
where attr != null && attr.Value == removeItem.ToString()
select item;
results.ToList().ForEach(item => item.Remove());
document.Save(fullFilePath);
}
}
我會盡量找藉口爲自己。我想我看不到樹木。當我重新審視這個問題時,我會喝我自己的處方藥。值得注意的不僅僅是因爲性能的提升,而是我可能不會注意到的一種簡化 - 感謝Jon – CarneyCode 2011-03-19 09:51:08