locked
Get last attribute in Xml file RRS feed

  • Question

  • User1661005202 posted

    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>

    Tuesday, November 30, 2010 12:12 AM

Answers

  • User960609757 posted

    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);





    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 30, 2010 3:13 AM

All replies

  • User960609757 posted

    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);





    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 30, 2010 3:13 AM
  • User-1129879462 posted

    using System.Xml;

    XmlDocument xmldoc = new XmlDocument();       
    xmldoc.Load("C:\\jer\\blog.xml");
    xmldoc.DocumentElement.LastChild.Attributes.Item(0).Value 

    Raj

    Tuesday, November 30, 2010 3:25 AM
  • User1661005202 posted

    Works great! Thank you very much for the help.

    Tuesday, November 30, 2010 3:52 AM