locked
XPath Invalid Token Error and get Node value RRS feed

  • Question

  • Hi!

    I have made a thread before. I want to get the foods of the boy with age=12, all three of them and put them in an array/nodelist. I want do this using an xpath.

    I have tried this Xpath    :         XmlNodeList snifa = repo.SelectNodes("person/boys/boy[@id = 12]/food/Value/Text()"); but I get an "XpathException and has invalid token" This is the xml file below. The thread I made earlier is here http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d512b5d3-a98d-4970-a26b-15d07fd41e4b

    Any ideas would be so helpful

     

     

    <person>

    <boys>

    <boy age = "12">

    <color>yellow</color>

    <food good="Y">

    <value>rice</value>

    </food>

    <food good="Y"> meat<\food>

    <value>meat</value>

    <food good="Y">

    <value>sausage</value>

    </food>

    </boy>

    </boys>

    </person>

    Tuesday, July 26, 2011 10:46 PM

Answers

  • Hi, I do for you this code...

     

    XmlDocument doc = new XmlDocument();
    doc.Load("Sample.xml");
    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;
    nodeList = root.SelectNodes("//person/boys/boy[@age = '12']");
    List<string> lstFood= new List<string>();
    foreach (XmlNode food in nodeList)
    {
       int counter = food.ChildNodes.Count;
       for (int i = 0; i < counter; i++)
       {
         try
         {
            string atribute = food.ChildNodes[i].Name.ToString();
            if (atribute == "food")
            {
              lstFood.Add(food.ChildNodes[i].InnerText);
            }
         }
         catch { }
       }
    }
    

    I test it and work fine.

    Please mark as an answer if it help you.

    Regards,


    Profesional Microsoft VIP | México
    Wednesday, July 27, 2011 4:00 AM
  • Three problems in your xpath:

    1) the 'boy' element doesn't have an 'id' attribute

    2) the 'value' element starts with a lower-case 'v'

    3) the 'text()' function starts with a lower-case 't' (this one causes the exception)

    XmlNodeList snifa = repo.SelectNodes("person/boys/boy[@age = 12]/food/value/text()");


    • Marked as answer by snif123 Friday, July 29, 2011 1:55 PM
    Wednesday, July 27, 2011 12:15 PM

All replies

  • Hi, I do for you this code...

     

    XmlDocument doc = new XmlDocument();
    doc.Load("Sample.xml");
    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;
    nodeList = root.SelectNodes("//person/boys/boy[@age = '12']");
    List<string> lstFood= new List<string>();
    foreach (XmlNode food in nodeList)
    {
       int counter = food.ChildNodes.Count;
       for (int i = 0; i < counter; i++)
       {
         try
         {
            string atribute = food.ChildNodes[i].Name.ToString();
            if (atribute == "food")
            {
              lstFood.Add(food.ChildNodes[i].InnerText);
            }
         }
         catch { }
       }
    }
    

    I test it and work fine.

    Please mark as an answer if it help you.

    Regards,


    Profesional Microsoft VIP | México
    Wednesday, July 27, 2011 4:00 AM
  • Three problems in your xpath:

    1) the 'boy' element doesn't have an 'id' attribute

    2) the 'value' element starts with a lower-case 'v'

    3) the 'text()' function starts with a lower-case 't' (this one causes the exception)

    XmlNodeList snifa = repo.SelectNodes("person/boys/boy[@age = 12]/food/value/text()");


    • Marked as answer by snif123 Friday, July 29, 2011 1:55 PM
    Wednesday, July 27, 2011 12:15 PM