[英]Querying an XML log with LINQ
我將所有更改記錄到使用FileWatcher的文件夾到XML文件
XDocument log = XDocument.Load(logpath);
XElement change = new XElement("change");
change.Add(new XAttribute("datetime", stamp));
change.Add(new XAttribute("status", "Deleted"));
change.Add(new XAttribute("filename", filepath));
log.Add(change);
log.Save(logpath);
然后,當我想查看某個時間戳所做的更改時,我用
var query = _filelog.Elements().Where(x => ((DateTime)x.Element("datetime")) <= time);
這只是XElements的IEnumerable,使用起來不太方便。 如何將其變成更方便的集合,例如鍵值對狀態字典列表:“已刪除”等? XElement中的XAttributes數量不同,具體取決於更改的類型。
此外,是否有一種方法可以查詢每個文件名的最新更改,直到時間戳記為止,而不是查詢直到該點為止的所有更改?
請嘗試以下解決方案之一。 首先是DateTime是唯一的,第二是是否有重復的DateTime值。
Dictionary<DateTime, XElement> dict1 = doc.Elements("change")
.GroupBy(x => DateTime.Parse(x.Attribute("datetime").Value), y => y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
Dictionary<DateTime, List<XElement>> dict2 = doc.Elements("change")
.GroupBy(x => DateTime.Parse(x.Attribute("datetime").Value), y => y)
.ToDictionary(x => x.Key, y => y.ToList());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.