Solved

Select an XML node with specific DateTime in attribute StartDateTime

Posted on 2010-11-08
4
754 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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