Solved

Select an XML node with specific DateTime in attribute StartDateTime

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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