Solved

Select an XML node with specific DateTime in attribute StartDateTime

Posted on 2010-11-08
4
738 Views
Last Modified: 2012-05-10
I have the following test:

[Test]
        public void FindTransactionByStartDateTime()
        {
            DateTime purchaseDate = new DateTime(2010, 7, 30, 15, 2, 8, 73);
            string customerId = "0460660450261";
            string xml = @"<GetHouseHoldFilteredTransactionsResponse xmlns='http://www.retalix.com/HQLWebServices'><GetHouseHoldFilteredTransactionsResult Status='Ok' /><out_HouseHoldTransactions><Transactions><Transaction xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' StoreId='6066' PosDateTime='2010-07-30' PosId='12' TranId='102' StartDateTime='2010-07-30T15:02:08.73' ClubCardId='0460660450261' CreatedAt='2010-07-30' UpdatedBy='1' TotalAmount='0.9900' StoreName='CH WOESTE' /></Transactions></out_HouseHoldTransactions></GetHouseHoldFilteredTransactionsResponse>";
            string transactionXml = SlipServiceLoyalty.GetTransactionXml(xml, customerId, purchaseDate);
            Assert.IsNotNullOrEmpty(transactionXml);
        }

public static string GetTransactionXml(string xml, string customerId, DateTime purchaseDate)
        {
            string date = purchaseDate.ToString("yyyy-MM-ddTHH:mm:ss.FF");

            XDocument doc = XDocument.Parse(xml);
            var results = (from t in doc.Descendants("{http://www.retalix.com/HQLWebServices}Transaction")

                           where IsMatchAttribute(t, "ClubCardId", customerId)
                           where (IsMatchAttribute(t, "StartDateTime", purchaseDate.ToString("yyyy-MM-ddTHH:mm:ss.FFF")) ||
                              IsMatchAttribute(t, "StartDateTime", purchaseDate.ToString("yyyy-MM-ddTHH:mm:ss.FF")))
                           select t).ToList();

            var result = results.FirstOrDefault();

            LogManager.GetCurrentClassLogger().DebugFormat("Found {0} transactions for customerId={1}", results.Count, customerId);
            return result != null ? result.ToString() : String.Empty;
        }

        private static bool IsMatchAttribute(XElement e, string attributeName, string value)
        {
            return (string)e.Attribute(attributeName) == value;
        }

Please bare in mind that the date could have FFF or FF format. I would like to catch both. My problem is that the condition on the date doesn't find the attribute StartDateTime.
0
Comment
Question by:breezback
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
Nirmalan Nagenthiran earned 300 total points
ID: 34090728
When you use
purchaseDate.ToString("yyyy-MM-ddTHH:mm:ss.FF") --> out put is 2010-07-30T15:02:08.07

Similarly
purchaseDate.ToString("yyyy-MM-ddTHH:mm:ss.FFF") --> out put is 2010-07-30T15:02:08.073

that is the reason it doesn't show the proper match.

Or else you can change this line like this
DateTime purchaseDate = new DateTime(2010, 7, 30, 15, 2, 8, 730); after this it works well

Also you can refer http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#fSpecifier for more details


0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 200 total points
ID: 34090732
You are passing 73 milliseconds as the value
>> DateTime purchaseDate = new DateTime(2010, 7, 30, 15, 2, 8, 73);
You should be passing 730 instead
Check with
DateTime purchaseDate = new DateTime(2010, 7, 30, 15, 2, 8, 730);
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 34090744
Sorry NNirmalan....didn't mean to repeat you..was near simulteneous
0
 
LVL 12

Expert Comment

by:Nirmalan Nagenthiran
ID: 34090810
It's fine arif_eqbal.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Problem Solving Execerscise 5 42
Data Saving 5 40
install app on other machine 13 14
Different Delete Messages 7 19
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now