Computer
Computer

Reputation: 2227

Parse datetime from XML element

var xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
                        <metadata created=""2014-11-03T18:13:02.769Z"" xmlns=""http://example.com/ns/mmd-2.0#"" xmlns:ext=""http://example.com/ns/ext#-2.0"">
                            <customer-list count=""112"" offset=""0"">
                                <customer id=""5f6ab597-f57a-40da-be9e-adad48708203"" type=""Person"" ext:score=""100"">
                                    <name>Bobby Smith</name>
                                    <gender>male</gender>
                                    <country>US</country>
                                    <date-span>
                                        <begin>1965-02-18</begin>
                                        <end>false</end>
                                    </date-span> 
                                </customer> 
                                <customer id=""22"" type=""Person"" ext:score=""100"">
                                    <name>Tina Smith</name>
                                    <gender>Female</gender>
                                    <country>US</country>
                                    <date-span>
                                        <end>false</end>
                                    </date-span> 
                                </customer>
                                <customer id=""30"" type=""Person"" ext:score=""500"">
                                    <name>George</name>
                                    <gender>Male</gender>
                                    <country>US</country>
                                    <date-span>
                                        <begin>1965</begin>
                                        <end>false</end>
                                    </date-span> 
                                </customer> 
                            </customer-list>    
                         </metadata>";

Im using the above XML. The problem i have is the date (im referring to the <date-span> <begin> element) can be in any format. So i am trying to use the below code in order to take care of the date format

GetCustomers = from c in XDoc.Descendants(ns + "customer")
                                  select
                                  new Customer
                                  {
                                      Name = c.Element(ns + "name").Value,
                                      DateOfBirth = Convert.ToDateTime(c.Element(ns + "date-span").Elements(ns + "begin").Any() ? c.Element(ns + "date-span").Element(ns + "begin").Value : DateTime.Now.ToString())
                                  };

The above works but crashed as soon as the XML contained 1965 - unfortunately i have no control over the XML. So i tried to use TryParse in order to convert 1965 to dd/mm/1965 where dd and mm could be todays date and current month, but i cant seem to get it working:

BeginDate = Convert.ToDateTime(c.Element(ns + "life-span").Elements(ns + "begin").Any() ? DateTime.TryParse( c.Element(ns + "life-span").Element(ns + "begin").Value, culture, styles, out dateResult) : DateTime.Now).ToString())

Could anyone guide me here how to resolve the issue?

Edit 1

var ModifyBeginDate = XDoc.Descendants(ns + "artist").Elements(ns + "date-span").Elements(ns + "begin");

The above retrieves all the dates but how do i assign the values after i have changed them back to the XML (i dont think i can use this variable in my code as when i iterate through the XML it would go directly back to the original XML)

Upvotes: 0

Views: 855

Answers (1)

caesay
caesay

Reputation: 17233

If the data can be in any format then you will have to preprocess the data before trying to parse it into a DateTime.

If i were going to implement this the first thing i would do is break the input into an array of integers, if there is only one item in the array i would check the length, if it was 4 long then I would assume that it is a year and instantiate a new DateTime of January 1, with the year. If i found an array of length 4,2,2 or 2,2,4 i would parse them accordingly - obviously there will be some of it left to guessing but if you can't control the format of the xml there will always be something left to chance

You could use something like this (but modified to only return the integer types and skip the splitting type which could be /,-, etc) to split the date time into an array containing the integer values: https://stackoverflow.com/a/13548184/184746

Upvotes: 1

Related Questions