从 XML 中最近的日期时间戳中获取 xmlnode

本文关键字:时间戳 获取 日期 xmlnode XML 最近 | 更新日期: 2023-09-27 18:32:48

>im试图从XML文件中获取XMLNODE"Price",XML本身就很大,并且有很多很多"行"元素。我正在尝试通过最新的"交易日期时间"来获取"价格"节点,因为它有时间戳,但我无法让它工作。

XmlDocument xdocoA = new XmlDocument();
xdocoA.Load(Transation);
XmlNodeList ndlistA = xdocoA.SelectNodes("/eveapi/result/rowset/row[@transactionDateTime]");
foreach (XmlNode xmlnodeA in ndlistA)
{
    LastTN.Text = xmlnodeA.Attributes["price"].InnerText;                               
}

XML 文件 :

<eveapi version="2"> 
  <currentTime>2016-02-01 22:48:26</currentTime>  
  <result> 
    <rowset name="transactions" key="transactionID" columns="transactionDateTime,transactionID,quantity,typeName,typeID,price,clientID,clientName,stationID,stationName,transactionType,transactionFor,journalTransactionID,clientTypeID"> 
      <row transactionDateTime="2016-01-31 23:10:57" transactionID="4212499228" quantity="12" typeName="Spodumain Mining Crystal II" typeID="18624" price="900000.00" clientID="94420021" clientName="Gayle Rowen" stationID="61000400" stationName="4F6-VZ XI - Limited Sense" transactionType="buy" transactionFor="personal" journalTransactionID="12205145551" clientTypeID="1373"/>  
      <row transactionDateTime="2016-01-30 17:52:03" transactionID="4210791656" quantity="1" typeName="Small Polycarbon Engine Housing I" typeID="31177" price="500000.00" clientID="95987816" clientName="Lash Wolfram" stationID="61000575" stationName="6-8QLA V - Perrigen Falls Trade Hub" transactionType="buy" transactionFor="personal" journalTransactionID="12198662373" clientTypeID="1376"/>
      <row transactionDateTime="2016-01-30 17:50:44" transactionID="4210790391" quantity="1" typeName="BZ-5 Neutralizing Spatial Destabilizer ECM" typeID="19946" price="549999.99" clientID="920370728" clientName="Missniggins" stationID="61000884" stationName="OP7-BP V - Ivy Towers" transactionType="buy" transactionFor="personal" journalTransactionID="12198656389" clientTypeID="1377"/> 
    </rowset> 
  </result>  
  <cachedUntil>2016-02-01 23:15:21</cachedUntil> 
</eveapi>

请记住,这个XML很大,这只是一个精简版本。

从 XML 中最近的日期时间戳中获取 xmlnode

XElement xml = XElement.Load("dat.xml");
var mostRecentPrice = xml.Descendants("row")
                         .OrderByDescending(r => DateTime.Parse(r.Attribute("transactionDateTime").Value))
                         .First().Attribute("price").Value;

您还可以按行的交易 ID 对行进行排序,前提是它们是升序的:

var mostRecentPrice = xml.Descendants("row")
                         .OrderByDescending(r => r.Attribute("transactionID").Value)
                         .First().Attribute("price").Value;

从评论中,我知道您在ndlistA中有Date s string s.

要获取最新事务,您可以使用以下 lambda:

var latestRowNode = ndlistA.Select(node => new { TimeStamp = DateTime.Parse(node.Attributes["transactionDateTime"]), RowNode = node, Price = node.Attributes["price"] })
                           .OrderBy(row => row.TimeStamp )
                           .Last();

在这里,最新的RowNode将具有:

latestRowNode.Row //Your row node
latestRowNode.TimeStamp // it's time stamp
latestRowNode.Price // it's price as string

根据注释,您希望获取给定事务日期XML中的特定节点。

以下代码可能会有所帮助。

XDocument doc = XDocument.Parse(s);     
var output = doc.Descendants("row") 
    .Where(e=>e.Attribute("transactionDateTime").Value == "2016-01-31 23:10:57")
    .Select (e=> new { 
        price = e.Attribute("price").Value, 
        quantity = e.Attribute("quantity").Value    
    });

如果您正在寻找最近的交易,则可以这样做。

var latestnode = doc.Descendants("row") 
.OrderByDescending(e=> DateTime.ParseExact(e.Attribute("transactionDateTime").Value,"yyyy-MM-dd HH:mm:ss",CultureInfo.InvariantCulture ))
.Select (e=> new { 
    price = e.Attribute("price").Value, 
    quantity = e.Attribute("quantity").Value,
    Date = e.Attribute("transactionDateTime").Value
}).First();

检查小提琴手演示和这个

使用 LINQ to-XML,可以直接将XAttribute转换为合适的 .NET 数据类型,例如:

var doc = XDocument.Load(Transation);
var latestRow = doc.Descendants("row")
                   .OrderByDescending(r => (DateTime)r.Attribute("transactionDateTime"))
                   .FirstOrDefault();
var latestPrice = (decimal)latestRow.Attribute("price");
Console.WriteLine(latestPrice);

dotnetfiddle demo

输出:

900000.00

参考 : XAttribute 显式转换运算符