2017-09-12 5 views
0

こんにちは、私はyyyy-mm-ddとしてフォーマットされているDateOfBirthキーフィールドがあり、ここでXML形式の日付のノードは、DD/MM/YYYYの#

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); 
} 

辞書には、このコードで私のXML文書を解析しています。 このフィールドをdd-mm-yyyyに再フォーマットする方法を教えてください。ここで

は私が唯一のフィールドを再フォーマットしたい言ったように、コード

-  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 

を実行した後のdictの値であるdateOfBirthの

答えて

1

それはDateOfBirth記録である場合は、各キーのチェックのための値を選択します。そうDateTimeに解析し、私はよりよい解決策を考えるしかし、希望の形式

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()); 

ためToStringを使用している場合はすべて一緒にこれらのプロパティを持つカスタムクラスを作成することになり、その後、あなたのXMLのうち、そのオブジェクトを作成します。

+0

を見て、私はその辞書からすべての値を必要とすると、彼らは文字列です。このエラーメッセージが表示される : '文字列が有効なDateTimeとして認識されませんでした。' –

+0

@СашкоМицевски - 質問を編集して、キーの外観に関するサンプルデータを表示してください –

+0

dictの値を編集して追加しました –