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

时间:2016-02-02 08:02:33

标签: c# xml

我试图从XML文件中获取XMLNODE“价格”,XML本身就是巨大的,并且其中包含许多“行”元素。我试图通过最新的“transactionDateTime”来获得“价格”节点,因为它有一个时间戳,但我无法让它工作。

<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文件:

WITH 
  MEMBER [Measures].[HC Threshold] AS 
    Val(StrToMember("[HC Threshold].[HC Threshold].[All].[25000]").Name) 
   ,FORMAT_STRING = "$#,0" 
  SET ClaimantsSet AS 
    Order
    (
      Filter
      (
        NonEmpty
        (
          [Count Of Claimants].[Count Of Claimants].[ID].MEMBERS
         ,{[Measures].[Plan Paid]}
        )
       ,
        [Measures].[Plan Paid] > [Measures].[HC Threshold]
      )
     ,[Measures].[Plan Paid]
     ,desc
    ) 
   SET [ICD9DescOrdered] AS
     Order
      (
        NonEmpty
        (
          [ICD-9 Primary Diagnosis Code].[Diagnosis Code].[ID].MEMBERS
         ,{[Measures].[Plan Paid]}
        )
       ,[Measures].[Plan Paid]
       ,asc
      )
  MEMBER [Measures].[ICD9Desc] AS 
    Tail
    (
      [ICD9DescOrdered]
    ).Item(0).Item(0).Properties("Short Description") 
SELECT 
  {[Measures].[ICD9Desc]} ON 0
 ,NON EMPTY 
    {ClaimantsSet} ON 1
FROM [Combined Claims]
WHERE 
  (
    [Insights Group Structure].[Insights Group Structure].[Insights Report ID].&[1706].FirstChild
    /* StrToMember("No HRA",CONSTRAINED), */
   ,[Plan Period].[Plan Period].[Date Year Quart].&[20152].Lead(4)
   ,[Claim Status].[Claim Status].[Claim Status ID].&[1]
  );
  

请记住这个XML很大,这只是一个缩减版本。

4 个答案:

答案 0 :(得分:1)

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;

答案 1 :(得分:0)

根据评论,您希望获得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();

检查提琴手demothis

答案 2 :(得分:0)

根据评论,我们了解到您Date中的stringndlistA

要获取最新的交易,您可以使用此lambda:

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

这里的latestRowNode会:

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

答案 3 :(得分:0)

使用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 Explicit Conversion Operators