xml format date node as dd/mm/yyyy #

Hi I am parsing my XML document with this code here

XDocument doc = XDocument.Load(path);

XElement person = doc.Descendants().Where(x => x.Name.LocalName == "Person").FirstOrDefault();
XNamespace ns = person.GetDefaultNamespace();

Dictionary<string, string> dict = person.Elements()
    .Where(x => x.Name.LocalName != "IdDocumentList")
    .GroupBy(x => x.Name.LocalName, y => y == null ? "" : (string)y)
    .ToDictionary(x => x.Key, y => y.FirstOrDefault());

foreach (XElement element in person.Descendants(ns + "IdDocument").FirstOrDefault().Elements())
{
    dict.Add(element.Name.LocalName, (string)element);
}

In the dict there is field key DateOfBirth that is formatted as yyyy-mm-dd. How can I will reformat this field to dd-mm-yyyy?

Here is the value of dict after executing the code

-       dict    Count = 14  System.Collections.Generic.Dictionary<string, string>
+       [0] {[PersonObjectId, 111111111]}   System.Collections.Generic.KeyValuePair<string, string>
+       [1] {[CellPhoneNo, 1212121212      ]}   System.Collections.Generic.KeyValuePair<string, string>
+       [2] {[DateOfBirth, 1971-03-06]} System.Collections.Generic.KeyValuePair<string, string>
+       [3] {[Email, [email protected]                                                     ]} System.Collections.Generic.KeyValuePair<string, string>
+       [4] {[EMBG, 12122121212]}   System.Collections.Generic.KeyValuePair<string, string>
+       [5] {[IsResident, 1]}   System.Collections.Generic.KeyValuePair<string, string>
+       [6] {[FirstName, xxx]}  System.Collections.Generic.KeyValuePair<string, string>
+       [7] {[GenderTypeId, 3]} System.Collections.Generic.KeyValuePair<string, string>
+       [8] {[LastName, xxxxx]} System.Collections.Generic.KeyValuePair<string, string>
+       [9] {[PhoneNo, ]}   System.Collections.Generic.KeyValuePair<string, string>
+       [10]    {[PlaceOfBirth, ]}  System.Collections.Generic.KeyValuePair<string, string>
+       [11]    {[IdDocumentTypeId, 2]} System.Collections.Generic.KeyValuePair<string, string>
+       [12]    {[PlaceOfIssue, xxxxx                                        ]} System.Collections.Generic.KeyValuePair<string, string>
+       [13]    {[IdNo, xxx]}   System.Collections.Generic.KeyValuePair<string, string>
+       Raw View        
        sql null    string

As i said i want to reformat only the field DateOfBirth

Upvotes: 0

Views: 842

Answers (1)

Gilad Green
Gilad Green

Reputation: 37281

When selecting the value for each key check if it is the DateOfBirth record. If so parse to DateTime and use ToString for the desired format

var dict = person.Elements()
    .Where(x => x.Name.LocalName != "IdDocumentList")
    .GroupBy(x => x.Name.LocalName, y => y == null ? "" : (string)y)
    .ToDictionary(x => x.Key, 
                  y => y.Key == "DateOfBirth" ? 
                       DateTime.ParseExact(y.FirstOrDefault(),"yyyy-mm-dd",null).ToString("dd-mm-yyyy") : 
                       y.FirstOrDefault());

However I think a better solution all together will be to create a custom class with these properties and then create an object of it out of your xml. Have a look at

Upvotes: 1

Related Questions