該代碼返回7個值。我只想檢索第7個值。選擇一個值的foreach值
你能給我一些建議嗎?
var a_nodes = root.Descendants("type");
foreach (var a_node in a_nodes)
{
Console.WriteLine("{0}", a_node.GetAttributeValue("value", ""));
}
該代碼返回7個值。我只想檢索第7個值。選擇一個值的foreach值
你能給我一些建議嗎?
var a_nodes = root.Descendants("type");
foreach (var a_node in a_nodes)
{
Console.WriteLine("{0}", a_node.GetAttributeValue("value", ""));
}
root.Descendants("type").Last()
或者:
root.Descendants("type").Skip(6).First();
即時得到此錯誤:在System.Core.dll中發生類型'System.InvalidOperationException'的未處理的異常 –
我相信它與此代碼無關。 –
第七節點
var lastnode = root.Descendants("type").Last();
如果a_nodes實現IEnumerable<T>
那麼你可以調用它的最後一個擴展方法來獲取列表中的最後一個項目。如果它實現IEnumerable
而不是IEnumerable<T>
那麼你可以調用Cast before Last。如果列表中沒有任何項目,則應調用LastOrDefault。
如果你不想使用LINQ或不能和a_nodes實現IList,那麼你可以得到計數,然後在索引1的項目比這少。
'root.Descendants(「type」)。Last()'? –