如何从节点组中查找最新的元素,其中元素的属性表示日期和时间.在xml中

how to find the latest element from the group of nodes where the attribute of the element represents the date and time. in xml

本文关键字:元素 日期 表示 属性 时间 xml 查找 最新 节点      更新时间:2023-09-26
<data>
  <manufacture  date="2013-06-05 T 19:40:50. 88463 7 Z">
    <title>java_package</title>
    <author>tom</author>
    <year>2013</year>
    <price>29.99</price>
  </manufacture>
  <manufacture  date="2015-06-05T19:40:50.884637Z">
    <title>java_package_2</title>
    <author>tom</author>
    <year>2015</year>
    <price>39.95</price>
  </manufacture>
 <manufacture  date="2014-06-05T19:40:50.884637Z">
    <title>java_package_3</title>
    <author>tom</author>
    <year>2003</year>
    <price>39.95</price>
  </manufacture>
</data>

DATA是根本要素,Manufacture是要素内容。在这里,我需要得到最新的制造,基于日期,即

2015-06-05 T 19:40:50。88463 7 Z

其是制造元素的属性。

我在asp.net和c#中工作

这是我尝试的代码

 XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml("bc.xml");
                XmlNode root = xdoc.DocumentElement;
              XmlNodeList nodeList = root.SelectNodes("//manufacture");
            foreach (XmlNode node1 in nodeList)
                {
                    Label1.Text +=  node.Attributes["DATE"].Value.ToString() + "'n <br>";
                }

文件中的日期时间格式错误且不一致。。。。应该是这样的:"2013-06-05 T19:40:50.884637Z"否则任何代码都会失败。。。

以下是解决方案。。。

var mf = doc.Root.Elements("manufacture")
        .OrderByDescending (m => DateTime.ParseExact(m.Attribute("date").Value, "yyyy-MM-dd THH:mm:ss.ffffffZ", null))
        .First();