Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get xmlnode from most recent datetime stamp in XML

Tags:

c#

xml

im trying to get XMLNODE "Price" from a XML File, the XML is huge by it's self and has many, many "Row" Elements in it. I'm trying to get the "Price" node, by going by latest "transactionDateTime" since it has a time stamp but im having trouble getting it to work.

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 File :

<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>

Please remember this XML is big and this is just a cut down version.

like image 903
Bic Pen Avatar asked Jun 23 '26 11:06

Bic Pen


1 Answers

XElement xml = XElement.Load("dat.xml");

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

You can also order the rows by their transaction IDs given they are ascending:

var mostRecentPrice = xml.Descendants("row")
                         .OrderByDescending(r => r.Attribute("transactionID").Value)
                         .First().Attribute("price").Value;
like image 176
w.b Avatar answered Jun 25 '26 02:06

w.b