Solved

Select an XML node with specific DateTime in attribute StartDateTime

Posted on 2010-11-08
4
739 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 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

17 Experts available now in Live!

Get 1:1 Help Now