LINQ:如何获取最后一个XML子节点? [英] LINQ: How to get the last XML Child Node?
问题描述
我的XML看起来像< g class =" gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace"数据-GR-ID =" 8英寸ID = QUOT; 8英寸;> XML< / g取代;下面:
如何获取最后一个节点?
我不想传递ID,而只是获取元素最后换色。
我做的是这样的,但它依赖于ID:
< pre class ="prettyprint"style =""> List< XElement> node =(from el in exampleParent.Descendants(" red")
其中el.Element(" id")。Value ==" 3"
select el)。ToList();
< color>
< red id =" 1">
< / red>
< red id =" 2">
< / red>
< red id =" 3">
< / red>
< / color>
列表 < XElement > 节点
< span style =""> = ( 来自 exampleParent "> $
style ="">后代 ( " red" ) < span style ="">
选择 ed ) .Last() ;
https://docs.microsoft.com/en- us / dotnet / api / system.linq.enumerable.last?view = netframework-4.7.2
I have an XML that looks like the <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="8" id="8">xml</g> below:
How do I get the last node?
I do not want to pass an ID but rather just get the element that is last for color.
I am doing something like this but it relies on the ID:
List<XElement> node = (from el in exampleParent.Descendants("red") where el.Element("id").Value == "3" select el).ToList();
<color> <red id="1"> </red> <red id="2"> </red> <red id="3"> </red> </color>
List<XElement> node = (from el in exampleParent.Descendants("red")
selected).Last();https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.last?view=netframework-4.7.2
这篇关于LINQ:如何获取最后一个XML子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!