Skip to content
Categories:

Using XPathDocument

Post date:
Author:
Number of comments: no comments

using Systm.XML.Xpath

//creates an instance of XPathDocument class and loads xml
if (System.IO.File.Exists(Server.MapPath(“/portal/BrowserCookieValidation.xml”)))
{
XPathDocument doc = new XPathDocument(Server.MapPath(“BrowserCookieValidation.xml”));
//An XPathNavigator object acts like a cursor, addressing a node in the XML document at a time
XPathNavigator nav = doc.CreateNavigator();
string message = String.Empty;
// Compile a standard XPath expression
XPathExpression expr = nav.Compile(“/CookieValidation/OutCompany[@OCID=” + OCID.ToString() + “]”);
//select node with ocid = 131
XPathNodeIterator iterator = nav.Select(expr);

//if node is found, count would be greater than 0 and company specific messsaage would come
if (iterator.Count > 0)
{ //Iterate through the selected nodes
while (iterator.MoveNext())
{
//if attribute is not there but node is there,null will be returned
if (iterator.Current.GetAttribute(“ErrorMessage”, string.Empty) != null)
message = iterator.Current.GetAttribute(“ErrorMessage”, string.Empty);
}
}
else //get default message
{
message = GetDefaultMessage(doc, nav);
}
//in case ocid node is there, but blank errormessage, bring default message
if (string.IsNullOrEmpty(message))
message = GetDefaultMessage(doc, nav);

//put value in the labbel
msgContainer.Text = message;


Meet Vinit: With over two decades of global experience in digital transformation and leadership coaching, Vinit is a seasoned catalyst for growth. Holding leadership positions at Adobe, Mindtree, HCL, and more, he's dedicated to driving personal and business development. Vinit, an IIM-K alumnus, is certified as PCC by ICF and also holds coaching credentials from Coacharya, the University of California and Toastmasters. As an ardent enthusiast of artificial intelligence and a proficient leadership coach, Vinit consistently bridges the gap between cutting-edge digital strategies and the cultivation of effective leadership skills.

Leave a Reply

Your email address will not be published. Required fields are marked *