1
我想选择具有最新时间戳(作为属性存储)的元素并删除可能存在的任何其他元素(因为这意味着用户与xml文件混淆)。使用XML.Linq从xml文档中删除过时的元素
(目录,名和文件名的字段和/或参数,以该方法包含该体)
using (FileStream fileStream = new FileStream(Path.Combine(directory, fileName), FileMode.OpenOrCreate))
{
XElement xmlRoot = XElement.Load(fileStream);
var query = from e in xmlRoot.Elements("key")
where e.Attribute("name")?.Value == name
orderby Int32.Parse(e.Attribute("timestamp")?.Value) descending select e;
if(query.Count() == 1)
{
return query.First();
}
else if (query.Count() > 1)
{
//here I need to delete any other than First (which has
//the latest timestamp, and return first, just like above)
}
else
{
}
}
具体而言,我想知道如何删除任何过时的元素(那些具有较小时间戳)内条款if (query.Count() > 1)
条款。 LINQ一直对我有点神秘。对不起,如果问题是微不足道的。
PS:我鼓励任何知道该问题的具体计算名称的人编辑并重命名我的问题,并根据需要对其进行重新说明。我是一个业余爱好者。
谢谢!我从来没有想到这会工作。 – FinnTheHuman
@FinnTheHuman,不客气;) – octavioccl