c# - XML 节点最新时间戳 C#

标签 c# xml linq duplicates xelement

我只需要打印同一个员工一次并检查它是否是最新的时间戳。我只需要一次员工 ID,但需要它的最新时间戳。

这是我目前的结果,使用不同的时间戳打印相同的 id(示例):

1 2015-03-16T21:32:30
1 2015-03-16T21:33:30
2 2015-03-16T21:32:30
3 2015-03-16T21:32:30
2 2015-03-16T21:33:30

这就是我的代码现在的样子。

  static void Main(string[] args)
        {
            XElement xelement = XElement.Load("data.xml");
            IEnumerable<XElement> employees = xelement.Elements();
            Console.WriteLine("List of Employee and latest timestamp:");
            foreach (var employee in employees)
            {
                Console.WriteLine("{0} has Employee ID {1}",
                    employee.Element("Employee").Value,
                    employee.Element("ChangeTimeStamp").Value);
            }
            Console.ReadKey();
        }

明确地说,我希望我的结果是:

1 2015-03-16T21:33:30
2 2015-03-16T21:33:30
3 2015-03-16T21:32:30

最佳答案

您需要按照 Employee 元素的值对员工节点进行分组,如下所示:

XElement xelement = XElement.Load("data.xml");
var employees = xelement.Elements()
            .Select(e => new 
            {
                Name = e.Element("Employee").Value,
                ChangeTimestamp = DateTime.Parse(e.Element("ChangeTimestamp").Value)
            })
            .GroupBy(e => e.Name)
            .Select( g => new 
            {
                Name = g.Key,
                ChangeTimestamp = g.Max(e => e.ChangeTimestamp)
            });

现在,当你迭代时,你应该有正确的值

foreach(var employee in employees)
{
    Console.WriteLine("{0} {1}", employee.Name, employee.ChangeTimestamp);
}

编辑我

要删除除最新版本之外的其他元素,您应该过滤它们并从其父级中删除每个元素。代码应如下所示:

XElement xelement = XElement.Load("data.xml");
xelement.Elements()
    .GroupBy(e => e.Element("Employee").Value)
    .SelectMany(g =>
    {
        var maxDate = g.Max( e => DateTime.Parse(e.Element("ChangeTimestamp").Value));
        return g.Where(e => DateTime.Parse(e.Element("ChangeTimestamp").Value) != maxDate);
    })
    .ToList()
    .ForEach(e=>e.Remove());

这当然不是最佳解决方案,但它为您提供了正确方向的提示。调整它以满足您的需要。

编辑二

粘贴箱中的文件有 3 个不同的员工,而不是一个员工的 3 个不同的 ChangeTimestamp。我尝试了以下代码片段并且它有效:

XDocument doc = XDocument.Load(@"path");
doc.Root.Elements("Employee")
    .GroupBy(e => e.Element("Employee").Value)
    .SelectMany(g=>
    {
        return g.OrderByDescending(e => DateTime.Parse(e.Element("ChangeTimeStamp").Value))
            .Skip(1);
    })
    .ToList()
    .ForEach(e=>e.Remove());

关于c# - XML 节点最新时间戳 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28894888/

相关文章:

c# - 请帮助我将 SQL 转换为 LINQ

c# - 在 C# 应用程序中处理位图

c# - 通过 View 模型图/树进行验证

java - Pom.xml:连接超时和模型版本丢失错误

java - 安卓.view.InflateException : Binary XML file line #10: Error inflating class fragment

.net - .NET 中的对象关系映射器和存储过程的使用

c# - Dictionary<Foo, List<Bar>> 如何使用 LINQ 将列表分配给键的属性?

c# - WCF 关注点分离 VS DRY

c# - 如何在 C# 中禁用图表控件的缩放?

sql - 使用来自 db2 的 SQL 从 XML Clob 中提取数据