日期:2014-05-18  浏览次数:20806 次

如何得到xml中最后一个<info>标签中的<id>和<timestamp>的属性值?
如下xml所示:如何得到xml中最后一个<info>标签中的<timestamp>的属性值,即timestamp=1312173361?
XML code

- <root>
- <data>
  <hasnext>1</hasnext> 
- <info>
  <id>118560075872631</id> 
  <timestamp>1330022743</timestamp> 
  </info>
- <info>
  <id>10775055490488</id> 
  <timestamp>1329439413</timestamp> 
  </info>
- <info>
  <id>102585001626915</id> 
  <timestamp>1323588512</timestamp> 
  </info>
- <info>
  <id>85617084111117</id> 
  <timestamp>1323396725</timestamp> 
  </info>
- <info>
  <id>90616081992310</id> 
  <timestamp>1323317410</timestamp> 
  </info>
- <info>
  <id>52621020669019</id> 
  <timestamp>1323133289</timestamp> 
  </info>
- <info>
  <id>9699010815682</id> 
  <timestamp>1321321209</timestamp> 
  </info>
- <info>
  <id>40664105746709</id> 
  <timestamp>1320896842</timestamp> 
  </info>
- <info>
  <id>79594093104019</id> 
  <timestamp>1320896776</timestamp> 
  </info>
- <info>
  <id>55101034977487</id> 
  <timestamp>1320896218</timestamp> 
  </info>
- <info>
  <id>68098006069005</id> 
  <timestamp>1320895842</timestamp> 
  </info>
- <info>
  <id>54101033311228</id> 
  <timestamp>1320894821</timestamp> 
  </info>
- <info>
  <id>38668097211444</id> 
  <timestamp>1320887989</timestamp> 
  </info>
- <info>
  <id>89594043056973</id> 
  <timestamp>1320836219</timestamp> 
  </info>
- <info>
  <id>35199130299086</id> 
  <timestamp>1320745725</timestamp> 
  </info>
- <info>
  <id>26160037617200</id> 
  <timestamp>1315538589</timestamp> 
  </info>
- <info>
  <id>89552116897653</id> 
  <timestamp>1315387425</timestamp> 
  </info>
- <info>
  <id>95523052545030</id> 
  <timestamp>1312947079</timestamp> 
  </info>
- <info>
  <id>11636067646985</id> 
  <timestamp>1312788015</timestamp> 
  </info>
- <info>
  <id>22141054836576</id> 
  <timestamp>1312685215</timestamp> 
  </info>
- <info>
  <id>25607081109756</id> 
  <timestamp>1312423867</timestamp> 
  </info>
- <info>
  <id>44049088666798</id> 
  <timestamp>1312358464</timestamp> 
  </info>
- <info>
  <id>73535113056798</id> 
  <timestamp>1312257547</timestamp> 
  </info>
- <info>
  <id>93018133245703</id> 
  <timestamp>1312173361</timestamp> 
  </info>
  <timestamp>1334285419</timestamp> 
  <totalnum>24</totalnum> 
  </data>
  <errcode>0</errcode> 
  <msg>ok</msg> 
  <ret>0</ret> 
  </root>


------解决方案--------------------
Linq的XDocument的LastNode ,看帮助
或者用XmlDocument的SelectNodes 找到一组,然后访问最后有一个索引的Node
------解决方案--------------------
看看xpath
XmlDocument x = new XmlDocument();
x.Load(@"c:\test.xml");
XmlNodeList xnl = x.SelectNodes("//data/info[last()]/timestamp");
foreach (XmlNode xn in xnl)
{
Console.WriteLine(xn.InnerText);