![]() |
Web Development »
ASP.NET »
Data
License: The Code Project Open License (CPOL)
Make More Efficient Access to XML Data with XPathNavigator ObjectBy David RosaThis 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
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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;
}
}
I hope you learned with this tutorial. If you have any questions, please send them to davidalexrosa@gmail.com. Thanks!
In the next article, we will use conditions and CDATA Sections with XPathNavigator. I hope you enjoy it as much as I do.
General
News
Question
Answer
Joke
Rant
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 Web20 | Advertise on the Code Project |