Using XPathDocument
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;
Vinit Kumar Singh
I write about leadership, execution and the transition from technical roles into organizational responsibility. My essays examine why capable teams struggle, why transformations stall, and how professionals grow from individual contributors into leaders. More about my background is on the About page. I read and respond to thoughtful responses. You can also reach me on LinkedIn.