Get Last Element in C# using XElement

拜拜、爱过 提交于 2019-11-28 14:19:30
R4j

Or try this to get XElement:

XDocument doc = XDocument.Load("yourfile.xml");          
XElement root = doc.Root;
Console.WriteLine(root.Elements("post").Last());

You can use LastNode property on root element:

XElement root = doc.Root;
XElement lastPost = (XElement)root.LastNode;
var doc = XDocument.Parse(xml);
var lastPost = doc.Descendants("post").Last();

Try this:

rootElement.Descendants().Last()

If you aren't sure there'll be any, you could also use LastOrDefault(). If there might be other elements besides within the , there's an overload of Descendants that will let you find just the posts you're looking for.

Try this

XDocument doc= XDocument.Load("path to xml");
var last=doc.Root.LastNode;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!