c# - xml format date node as dd/mm/yyyy # -


hi parsing xml document 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 dict there field key dateofbirth formatted yyyy-mm-dd. how can reformat field dd-mm-yyyy?

here value of dict after executing 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, xxxxx@yahoo.com                                                     ]} 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 said want reformat field dateofbirth

when selecting value each key check if dateofbirth record. if parse datetime , use tostring 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 think better solution create custom class these properties , create object of out of xml. have @


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -