using xpath get the correct value in selectName

Posted by Sangeeta on Stack Overflow See other posts from Stack Overflow or by Sangeeta
Published on 2009-09-18T19:27:24Z Indexed on 2010/05/26 17:01 UTC
Read the original article Hit count: 292

Filed under:

Below I have written the code. I need help to get the right value in selectName. I am new to XPath. Basically with this code I am trying to achieve if employeeName = Chris I need to return 23549 to the calling function. Please help.

Part of the code:

public static string getEmployeeeID(string employeeName)
{

    Cache cache = HttpContext.Current.Cache;
    string cacheNameEmployee = employeeName + "Tech";

if (cache[cacheNameEpm] == null)
        {
            XPathDocument doc = new XPathDocument(HttpContext.Current.Request.MapPath("inc/xml/" + SiteManager.Site.ToString() + "TechToolTip.xml"));
            XPathNavigator navigator = doc.CreateNavigator();
            string selectName = "/Technologies/Technology[FieldName='" + fieldName + "']/EpmId";
            XPathNodeIterator nodes = navigator.Select(selectName);

            if (nodes.Count > 0)
            {
                if (nodes.Current.Name.Equals("FieldName"))
                //nodes.Current.MoveToParent();
                //nodes.Current.MoveToParent();
                //nodes.Current.MoveToChild("//EpmId");

                cache.Add(cacheNameEpm, nodes.Current.Value, null, DateTime.Now + new TimeSpan(4, 0, 0), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
            }
        }
        return  cache[cacheNameEpm] as string;
    }

XML file

<?xml version="1.0" encoding="utf-8" ?>

<Employees>
  <Employee>
    <EName>Chris</EName>
    <ID>23556</ID>
  </Employee>
  <Employee>
    <EName>CBailey</EName>
    <ID>22222</ID>
  </Employee>
  <Employee>
    <EName>Meghan</EName>
    <ID>12345</ID>
  </Employee>
</Employees>

PLEASE NOTE:This XML file has 100 nodes. I just put 1 to indicate the structure.

© Stack Overflow or respective owner

Related posts about xpath