2016-02-02 60 views
3

即时尝试从XML文件中获取XMLNODE“价格”,这个XML本身就是巨大的,它有很多“行”元素。我试图通过最新的“transactionDateTime”来获得“价格”节点,因为它有一个时间戳,但我很难让它工作。从XML中获取xmlnode最近的日期时间戳记

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大,这仅仅是一个减少的版本。

+1

你到底是遇到了问题与什么?解析时间戳?或者找到最近的一个? –

+1

请注意,'rowset'元素没有结束标记 – har07

+1

如果您对LINQ感到满意,请尝试将Linq转换为xml api。 – VivekDev

回答

0

由于根据意见,要获得在XML特定节点对于给定transactiondate

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

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

+1

但是当这个XML更新和时间戳更改日期/时间将在今年秋天过吗? –

+0

@BicPen你想在任何情况下选择最新的? –

+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; 
0

从评论,我知道你有在ndlistA中的Date s为strings。

要获取最新的事务,您可以使用此拉姆达:

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 
0

使用LINQ到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