In xml document, I want to get the Bottom xml node, how can I get the last xml nodes
<Books>
<book>
<author> sasi </author>
<pdate>2013-01-02</pdate>
</book>
<book>
<author> surya</author>
<pdate> 2013-02-02</pdate>
</book>
<book>
<author>dolly</author>
<pdate> 2013-04-01</pdate>
</book>
</Books>
from the above I want get the last <book>
node in the xml document.
Try this:
var xml = @"<Books>
<book>
<author> sasi </author>
<pdate>2013-01-02</pdate>
</book>
<book>
<author> surya</author>
<pdate> 2013-02-02</pdate>
</book>
<book>
<author>dolly</author>
<pdate> 2013-04-01</pdate>
</book>
</Books>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var node = doc.FirstChild.LastChild;
Console.WriteLine(node.OuterXml);
Outputs:
<book><author>dolly</author><pdate> 2013-04-01</pdate></book>
Alternatively, you may select the last book
child under the Books
element:
doc.SelectSingleNode("Books/book[last()]")
or the last book
element no matter where they are in the document:
doc.SelectSingleNode("//book[last()]");