Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

A better way to query for a single node
[ All Languages » VB.net »  XML]

Total Hit ( 2514)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


In the XmlNode class, that is the class that provides node functionality in an XMLDOM representation of an XML document, the SelectNodes (and the ancillary SelectSingleNode) method exploits the XPath query language to let you extract nodes based on logical conditions. The SelectNodes methods return a collection of XmlNode object, whereas SelectSingleNode returns only the first node that match the XPath criteria. However, a quick look at the implementation of SelectSingleNode suggests that using SelectNodes is often preferable. The following pseudocode shows the internal working of SelectSingleNode:

Click here to copy the following block
Public Function SelectSingleNode(xpathExpr As String) As XmlNode
  Dim nodes As XmlNodeList = SelectNodes(xpath)
  Return nodes(0)
End Funtion

The SelectSingleNode method internally calls SelectNodes and retrieves all the nodes that match a given XPath expression. Next it simply returns the first selected node to the caller. Using SelectSingleNode perhaps results in a more easily readable code, but doing so certainly does not improve the performance of the application when you need just one node. A better way to query for a single node is using a smarter XPath expression. The idea is that you place a stronger filter on the XPath expression so that it returns just the node, or the nodes, you want. For example, to get only the first node, use the following query:

Click here to copy the following block
doc.SelectSingleNode("NorthwindEmployees/Employee[position() = 1]")

The use of SelectSingleNode in this case simply serves the purpose of returning a XmlNode object rather than an array of objects.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.