Click here to Skip to main content
6,595,444 members and growing! (17,477 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Data License: The Code Project Open License (CPOL)

Make More Efficient Access to XML Data with XPathNavigator Object

By David Rosa

This article aims to demonstrate the usefulness of the XPathNavigator object to access the XML data, more quickly and efficiently.
C#, XML.NET 2.0, ASP.NET
Posted:12 Oct 2008
Views:5,647
Bookmarked:22 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.80 Rating: 2.67 out of 5

1
1 vote, 50.0%
2

3
1 vote, 50.0%
4

5

Introduction

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.

Background

  • 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"?>
<Configurations>
<AccessConfigurations>
<Username><![CDATA[da61r6kM29/7Q5GIQVDaDA==]]></Username>
<Password><![CDATA[2yGGVQ19TmC0hvL+gwXdgw==]]></Password>
</AccessConfigurations>
</Configurations>
//This is a helper class, which encapsulates our credentials.

[Serializable]
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; }
}

#endregion

#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;
}

#endregion
}

//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>();
try
{
string fileUrl = @".\TestFiles\Test.xml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(server.MapPath(fileUrl));
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 davidalexrosa@gmail.com. Thanks!

History

  • 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.

License

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

About the Author

David Rosa


Member
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.
Occupation: Engineer
Company: GISP Software, SA.
Location: Portugal Portugal

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Oct 2008
Editor: Deeksha Shenoy
Copyright 2008 by David Rosa
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project