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;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s