Click here to Skip to main content
12,998,776 members (70,130 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 12 Oct 2008

Use XpathNavigator to navigate and edit XML data

, 12 Oct 2008
Rate this:
Please Sign up or sign in to vote.
Use the XPathNavigator to edit and read XML. This article demonstrates how you can do, using conditions.


Use the XPathNavigator to read XML. This article demonstrates how you can do, using conditions.


- ASP.NET, C# Medium Knowledge.

- XML and XPATH Knowledge.

Using the code

This article demonstrates how to read data from a XML File, using XPATH. So, let the words aside and go to work.

This is the XML Source:

<?xml version="1.0" encoding="utf-8" ?>
<Role name="Admin">
<Username><![CDATA[<span class="code-SummaryComment">hGUC0QB2G3l1cb0YSnJkUJpcQB2cv6h3]]></span></Username>
<Password><![CDATA[<span class="code-SummaryComment">P3W+pDC2Vn/TH1Bbgmm/1A==]]></span></Password>
//Next, this is the method to get values from XML file:

private MyCredential[] GetAdminCredentials()
HttpServerUtility server = HttpContext.Current.Server;
List<MyCredential> credentials = new List<MyCredential>();
XmlDocument xmlDocument = new XmlDocument();
string filename = @".\Roles.xml";
XPathNavigator nav = xmlDocument.CreateNavigator();
XPathNodeIterator iterator = nav.Select("Roles/Role[@name='Admin']"); //this is a condition. Only nodes which contains role name = 'admin' are processed.
while (iterator.MoveNext()) //while there are nodes where role name='admin'
MyCredential credential = new MyCredential();
credential.Username = CryptographyServices.Decrypt(iterator.Current.Value);
iterator.Current.MoveToParent(); //move to root
iterator.Current.MoveToChild("Password", string.Empty);
credential.Password = CryptographyServices.Decrypt(iterator.Current.Value);
iterator.Current.MoveToParent(); //move to root again
return credentials.ToArray();
catch (Exception innerException)
throw innerException;


Points of Interest

In next article we will update a xml file using CData Sections. Thanks for your attention.


Keep a running update of any changes or improvements you've made here.


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

GeneralThanks Pin
AboShehab_FCIS28-Oct-09 2:19
memberAboShehab_FCIS28-Oct-09 2:19 
GeneralUse Linq to XML instead Pin
WileCoyot13-Oct-08 10:10
memberWileCoyot13-Oct-08 10:10 
GeneralRe: Use Linq to XML instead Pin
baudhayana1-Sep-09 21:05
memberbaudhayana1-Sep-09 21:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 12 Oct 2008
Article Copyright 2008 by David Rosa
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid