simple xpath expressions question

I use C#. This is my XML file.

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

<Schedules>

<Schedule Date="14/12/2005">

<Location>Place1</Location>

<Time>12:00</Time>

<Description>Set mode</Description>

</Schedule>

<Schedule Date="15/12/2005">

<Location>Place2</Location>

<Time>01:00</Time>

<Description>Select Position</Description>

</Schedule>

</Schedules>

How do I query this to extract data based on criteria I would then like to assign it to a dataset and returned to be loaded on to a datagrid.

This is my code -

XPathNavigator nav;

XPathDocument docNav;

XPathNodeIterator nodeIter;

string expr;

docNav=new XPathDocument(XML_SCHEDULE_FILE);

nav=docNav.CreateNavigator();

expr="/Schedules/Schedule/[Location=Place1]";

nodeIter=nav.Select(expr);

while(nodeIter.MoveNext())

{

string s=nodeIter.ToString();

}

Whatever expression I use, it seems to return a runtime error - "The expression passed to this method should result in a NodeSet".

Can someone please post the right way to query my xml based on a criteria which also includes "and" operator.
 
Thanks




Answer this question

simple xpath expressions question

  • dmitry.bulavin

    Hi,

    http://www.w3schools.com/xpath/xpath_functions.asp#datetime

    Use the date-time functions shown in above link to manipulate your conditions...

    HTH,



  • Feitlebaum

    Hello there,

    Now, I would like to select values with date greater than the current date. Do you know how to do this   Any help is much appreciated.



  • jboekhoff

    Hi,
    I see in your code that you are using

    XPathNodeIterator
     
    which requires a node list and you are using

    nav.Select
     
    method which will give you only a signle node.
    So you will need to use method one of these -


    SelectAncestors, SelectChildren, SelectDescendants

     

    HTH,


  • Yian

    Indian Ocean, those are XPath 2.0 functions. Microsoft doesn't support XPath 2.0 yet. XPath 2.0 itself is still work in progress.

  • mhojlund

    This helped. Thanks.

  • Rob Kenny - MSFT

    This is cumbersome in XPath 1.0 as it doesn't support date time type. You have to parse your date value using substring function, make it YYYYMMDD, convert to number using number() function and then compare.

    Alternatively use EXSLT.NET, which extends XPah and XSLT with date time functions.

    1. EXSLT.NET homepage

    2. Article at MSDN with samples of date time manipulations

    3. eXml - extended XML web server control that supports EXSLT.



  • Johannes Hartmann

    expr="/Schedules/Schedule/[Location=Place1]";

    It must be

    expr="/Schedules/Schedule/[Location='Place1']";


    string
    s=nodeIter.ToString();

    And this one must be

    string
    s=nodeIter.Current.Value;

    Operator and in XPath is just "and", e.g.
    /Schedules/Schedule/[Location='Place1' and @Date='14/12/2005']
    this selects all Schedule elements having Location child with value 'Place1' and Date attributte with value '14/12/2005'.



  • simple xpath expressions question