Introduction
Use the XPathNavigator to read XML. This article demonstrates how you can do, using conditions.
Background
- 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:
="1.0"="utf-8"
<Roles>
<Role name="Admin">
<Username><![CDATA[</Username>
<Password><![CDATA[</Password>
</Role>
</Roles>
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']");
while (iterator.MoveNext())
{
MyCredential credential = new MyCredential();
iterator.Current.MoveToChild("Username",string.Empty);
credential.Username = CryptographyServices.Decrypt(iterator.Current.Value);
iterator.Current.MoveToParent();
iterator.Current.MoveToChild("Password", string.Empty);
credential.Password = CryptographyServices.Decrypt(iterator.Current.Value);
credentials.Add(credential);
iterator.Current.MoveToParent();
}
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.
History
Keep a running update of any changes or improvements you've made here.
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.