![]() |
Web Development »
ASP.NET »
Data
Beginner
License: The Code Project Open License (CPOL)
Use XpathNavigator to navigate and edit XML dataBy David RosaUse the XPathNavigator to edit and read XML. This article demonstrates how you can do, using conditions. |
C# (C# 2.0, C# 3.0), XML, .NET (.NET 2.0, .NET 3.0), ASP.NET
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
- ASP.NET, C# Medium Knowledge.
- XML and XPATH Knowledge.
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" ?>
<Roles>
<Role name="Admin">
<Username><![CDATA[hGUC0QB2G3l1cb0YSnJkUJpcQB2cv6h3]]></Username>
<Password><![CDATA[P3W+pDC2Vn/TH1Bbgmm/1A==]]></Password>
</Role>
</Roles>
//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>();
try
{
XmlDocument xmlDocument = new XmlDocument();
string filename = @".\Roles.xml";
xmlDocument.Load(server.MapPath(filename));
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();
iterator.Current.MoveToChild("Username",string.Empty);
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);
credentials.Add(credential);
iterator.Current.MoveToParent(); //move to root again
}
return credentials.ToArray();
}
catch (Exception innerException)
{
throw innerException;
}
}
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.
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 Oct 2008 Editor: |
Copyright 2008 by David Rosa Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |