Click here to Skip to main content
12,996,367 members (70,630 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 12 Oct 2008

Make More Efficient Access to XML Data with XPathNavigator Object

, 12 Oct 2008
Rate this:
Please Sign up or sign in to vote.
This article aims to demonstrate the usefulness of the XPathNavigator object to access the XML data, more quickly and efficiently.


Sometimes, in software projects we need to make the access to data more quickly and efficiently. So, there is the usefulness of the XPathNavigator object, which allows us to access the XML data more efficiently, quickly and in an easier way. Take a look.


  • ASP.NET C# initial/medium knowledge
  • XML, XPATH knowledge

Using the Code

Given below is the source code, you can use it and even improve it. I hope that helps you in the future.

We'll try to parse the next XML document:

<?xml version="1.0" encoding="UTF-8"?>
//This is a helper class, which encapsulates our credentials.

public class MyCredential
#region Properties.

/// <summary>
/// Represents the Username.
/// </summary>

private string _username;
public string Username
get { return _username; }
set { _username = value; }

/// <summary>
/// Represents the Password.
/// </summary>

private string _password;
public string Password

get { return _password; }
set { _password = value; }


#region Constructors.

/// <summary>
/// Default Constructor.
/// </summary>

public MyCredential()

/// <summary>
/// Constructor with parameters.
/// </summary>
/// <param name="username">Represents the username.</param>
/// <param name="password">Represents the password.</param>

public MyCredential(string username, string password)
    this.Username = username;
    this.Password = password;


//Method to parse the XML. This method could be executed on a Page for example 
//or inside a business service, which is more cool. 
//Use SOA architecture to do this (I prefer). This method should be at DAL. 

public MyCredential[] GetAccessConfigurations()
HttpServerUtility server = HttpContext.Current.Server;  
string username = string.Empty;
string password = string.Empty;
List<MyCredential> credentials = new List<MyCredential>();
string fileUrl = @".\TestFiles\Test.xml";
XmlDocument xmlDocument = new XmlDocument();
XPathNavigator nav = xmlDocument.CreateNavigator();
username = nav.SelectSingleNode("Configurations/AccessConfigurations/Username").Value;
password = nav.SelectSingleNode("Configurations/AccessConfigurations/Password").Value; 
credentials.Add(new MyCredential(username, password));
return credentials.ToArray();
}catch (Exception innerException)
throw innerException;

Points of Interest

I hope you learned with this tutorial. If you have any questions, please send them to Thanks!


  • 12th October, 2008: Initial post

In the next article, we will use conditions and CDATA Sections with XPathNavigator. I hope you enjoy it as much as I do.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

David Rosa
Engineer GISP Software, SA.
Portugal Portugal
Degree in Computer Engineering at the Autonomous University of Lisbon - Portugal (UAL - Universidade Autónoma de Lisboa).
Worked at Unisys Portugal in ASP.NET 2.0 Technology and Microsoft Office SharePoint Server 2007 not forgetting adjacent technologies.
Worked also as Technical Coordinator of the GMTS's (Global Media Technology Solutions) Junior Development Team in the Portals Area, using Microsoft Office SharePoint Server 2007, ASP.NET, WPF, Silverlight and Frameworks 2.0, 3.0 and 3.5.
Now,he is working for GISP Software SA, a software house focused in the media market, as Software Engineer.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170609.2 | Last Updated 12 Oct 2008
Article Copyright 2008 by David Rosa
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid