Results 1 to 4 of 4

Thread: xml date compare

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    50

    xml date compare

    Hi All,

    I want to write a function that select some of xml nodes from a xml file
    that xml file has a attribute is date 20/06/2011 12:00:00 PM
    if i want to select all the node that is before 20/06/2011.

    i know sql query has >>> date LIKE "20/06/2011%"

    does xpath or .net has kind of method

    thank you

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: xml date compare

    If you want to compare dates temporally then you will need to create Date objects from the Strings contained in the XML. Depending on the circumstances, you might use CDate, Date.Parse, Date.ParseExact, Date.TryParse or Date.TryParseExact. You can then compare them like you would any other Dates, using mathematical operators.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    50

    Re: xml date compare

    sorry, my question was abit confuse.

    I want something like this>>
    Code:
     xmlnodelist = xmldoc.SelectNodes("/namelist/names[@dir='20/06/2011 12:00:00 PM']/name")
    that means it selects all the name nodes which @dir = 20/06/2011 12:00:00 PM

    Now if I want to select all the name nodes which @dir LIKE '20/06/2011%' << I know the xml doesnt have LIKE, does any other simliar method that xml provide

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,420

    Re: xml date compare

    you could use LINQ to XML:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim xml = _
    5.         <?xml version="1.0" standalone="no"?>
    6.         <root>
    7.             <nameList>
    8.                 <name dir="20/06/2011 12:00:00 PM">name1</name>
    9.                 <name dir="20/06/2011 01:00:00 PM">name2</name>
    10.                 <name dir="20/06/2011 02:00:00 PM">name3</name>
    11.                 <name dir="21/06/2011 12:00:00 PM">name4</name>
    12.                 <name dir="21/06/2011 02:00:00 PM">name5</name>
    13.             </nameList>
    14.         </root>
    15.  
    16.         MsgBox((From node In xml...<nameList>.<name> Where [email protected]("20/06/2011") Select node.Value) _
    17.                .Aggregate(Function(current, s) current & ", " & s).ToString)
    18.  
    19.     End Sub
    20. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width