none
LINQ to XML Getting records from StartDate to EndDate

    Question

  • I am using linq to XML in C#

    I have an XML where records are stored

    <ID>1</ID>
    <Date>01/12/2012 </Date>
    <value>10</value>

    <ID>2</ID>
    <Date>02/12/2012 </Date>
    <value>10</value>

    <ID>3</ID>
    <Date>03/12/2012 </Date>
    <value>10</value>

    I am trying to get the records for selected FromDate to ToDate.
    for example between fromDate > 01/12/2012 & endDate < 03/12/2012

    so I am writing the query as

     var r = from pro in d.Descendants("products")
                            where
                                (DateTime)pro.Element("Date") < Convert.ToDateTime(dateTimePickerToDate.Text) && (DateTime)pro.Element("Date") > Convert.ToDateTime(dateTimePickerFromDate.Text)                        
                            select pro;
                 
                    foreach (var p in r)
                    {
                        currentproductID = p.Element("ID").Value;                    
                    }

    but it is giving an error "string not recognized as a valid datetime"

    Any idea why I am getting an error?
    Thursday, August 01, 2013 7:00 PM

Answers

  • You should compare with the Value property of the XElement. Also, you probably want to use the Value property of the DatePicker controls. Here's an example:

    Data.xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <Products>
      <Product>
        <ID>1</ID>
        <Date>01/12/2012 </Date>
        <value>10</value>
      </Product>
      <Product>
        <ID>2</ID>
        <Date>02/12/2012 </Date>
        <value>10</value>
      </Product>
      <Product>
        <ID>3</ID>
        <Date>03/12/2012 </Date>
        <value>10</value>
      </Product>
    </Products>

    LINQ:

                    XDocument xdoc = XDocument.Load("Data.xml");
                    var products = (from product in xdoc.Descendants("Product")
                                    where Convert.ToDateTime(product.Element("Date").Value) < dateTimePickerToDate.Value
                                    && Convert.ToDateTime(product.Element("Date").Value) > dateTimePickerFromDate.Value
                                    select product).ToList();

    Thursday, August 01, 2013 8:06 PM

All replies

  • Can you share your original xml i mean complete xml, because your xml not in correct format.

    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Thursday, August 01, 2013 7:18 PM
  • You should compare with the Value property of the XElement. Also, you probably want to use the Value property of the DatePicker controls. Here's an example:

    Data.xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <Products>
      <Product>
        <ID>1</ID>
        <Date>01/12/2012 </Date>
        <value>10</value>
      </Product>
      <Product>
        <ID>2</ID>
        <Date>02/12/2012 </Date>
        <value>10</value>
      </Product>
      <Product>
        <ID>3</ID>
        <Date>03/12/2012 </Date>
        <value>10</value>
      </Product>
    </Products>

    LINQ:

                    XDocument xdoc = XDocument.Load("Data.xml");
                    var products = (from product in xdoc.Descendants("Product")
                                    where Convert.ToDateTime(product.Element("Date").Value) < dateTimePickerToDate.Value
                                    && Convert.ToDateTime(product.Element("Date").Value) > dateTimePickerFromDate.Value
                                    select product).ToList();

    Thursday, August 01, 2013 8:06 PM