0
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)

Computer
  • 2,149
  • 7
  • 34
  • 71
  • *"So i tried to use TryParse but i cant seem to get it working"* what exactly isn't working? – Yuval Itzchakov Nov 28 '14 at 21:21
  • Original thread updated to make it more clearer – Computer Nov 28 '14 at 21:30
  • You mean you it always returns `DateTime.Now`? – Yuval Itzchakov Nov 28 '14 at 21:52
  • Sorry don't quite follow what your asking but all I'm trying to do is if the date is 1965 from the XML then the BeginDate would convert it to 01/12/1965. It doesn't matter what the day or month is. At present the XML can contain a proper formatted date or just the year. I can get the proper date value using the first code snippet but it crashes when the XML returns just the year – Computer Nov 28 '14 at 22:10
  • Just a year isn't a proper `DateTime`. You'll need to priorly detect what kind of string you're about to handle. You should look into [`TryParseExact`](http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx) – Yuval Itzchakov Nov 28 '14 at 22:23
  • Break your convert statement into simpler statements so you can a) tell where the problem might be, and b) validate and handle improper data – dbugger Nov 28 '14 at 22:34

1 Answers1

1

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

Community
  • 1
  • 1
caesay
  • 16,932
  • 15
  • 95
  • 160
  • If ive understood you correctly then what im planning on doing is to get the value into a variable and then modify that - the link you posted refers to Java but im using the same concept but where i feel comfortable. That said i hit a snag :-( Im trying to get the date value into the new variable (code posted under edit 1). This gets all the dates into an IEnumerable, but then im not sure how to change the date back to the XML variable im reading? – Computer Dec 03 '14 at 20:30
  • @Computer: The link i posted is C#, look more carefully. I'm not really sure I understand your question? – caesay Dec 03 '14 at 23:48