welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, July 21, 2011

How to find the maximum node value in XML


I have a requirement to find the maximum number of id in xml node..Y means If I want to add some more node then we need the maximum node then only we can update the next node by using code..then I goggled to find the method, mostly I found result would be loop process to find the maximum id,I think that one very lenthy process ,bcz  xml has more nodes..its took waste time if we use for loop, one of my net friend gave me easiest process to find the maximum node. So I just thought share the code with you all………..
To remove particular code refer below link..
This is sample xml
Here we are going to find the maximum ‘id‘..Just follow the simple code I mention below….

Dim xmldoc As New XmlDocument

Dim max As String

            Dim nav As XPathNavigator = xmldoc.CreateNavigator()
            Dim sort As XPathExpression = nav.Compile("picture/image")
            sort.AddSort("ID", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Number)
            max = nav.SelectSingleNode(sort).SelectSingleNode("ID").Value

The result maximum value could be ‘3’
            max =3
I hope this will helpful those who seek to find max id in xml
Post your comments about the post……….


Post a Comment