I am using Asp.net 2.0 and not using linq. Is it possible to get an attribute of the last element in an XML file. I want to get the last elements attribute "articleId" value (4). Now the last number value will change with each new article added to the
Xml file. What's the best way to read through an XML file find the last "content" entry and get its article attribute value.
devinmccloud
Member
470 Points
151 Posts
Get last attribute in Xml file
Nov 30, 2010 04:12 AM|LINK
I am using Asp.net 2.0 and not using linq. Is it possible to get an attribute of the last element in an XML file. I want to get the last elements attribute "articleId" value (4). Now the last number value will change with each new article added to the Xml file. What's the best way to read through an XML file find the last "content" entry and get its article attribute value.
<blog>
<content articleId="1" category="" img="" month="" title="" user=""
date="" Description="" >
</content>g>
<content articleId="2" category="" img="" month="" title="" user=""
date="" Description="" >
</content>
<content articleId="3" category="" img="" month="" title="" user=""
date="" Description="" >
</content>
<content articleId="4" category="" img="" month="" title="" user=""
date="" Description="" >
</content>
</blog>
gopalanmani
Star
7826 Points
1320 Posts
Re: Get last attribute in Xml file
Nov 30, 2010 07:13 AM|LINK
Hi,
Try to like this,
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("content");
int NodeCount = xmlnode.Count;
XmlAttributeCollection xmlattrc = xmlnode[NodeCount-1].Attributes;
Console.WriteLine(xmlattrc["articleId"].Value);
Gopalan Mani
My Tech blog
krisrajz
Participant
1845 Points
541 Posts
Re: Get last attribute in Xml file
Nov 30, 2010 07:25 AM|LINK
using System.Xml;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("C:\\jer\\blog.xml");
xmldoc.DocumentElement.LastChild.Attributes.Item(0).Value
Raj
Raj
Remember to click Mark as Answer on the post that helps to others.
devinmccloud
Member
470 Points
151 Posts
Re: Get last attribute in Xml file
Nov 30, 2010 07:52 AM|LINK
Works great! Thank you very much for the help.