2013-03-19 46 views
1

我有我試圖用LINQ讀取生成的對象列表一些站XML:如何使用LINQ to XML創建對象列表?

<SCResponse> 
<link href="http://192.168.6.126:8001/affiliate/account/81/notifications?startRow=2&amp;limit=20" rel="next_page" title="Next"/> 
<link href="http://192.168.6.126:8001/affiliate/account/81/notifications?startRow=1&amp;limit=20" rel="previous_page" title="Previous"/> 
<RecordLimit>20</RecordLimit> 
<Notifications> 
    <Notification href="http://192.168.6.126:8001/affiliate/account/81/notifications/24"> 
     <NotificationDate>2013-03-15T16:41:37-05:00</NotificationDate> 
     <NotificationDetails>Notification details 1</NotificationDetails> 
     <Status>New</Status> 
     <NotificationTitle>Test notification 1</NotificationTitle> 
    </Notification> 
</Notifications> 
<RecordsReturned>1</RecordsReturned> 
<StartingRecord>1</StartingRecord> 
<TotalRecords>1</TotalRecords> 

而且我已經創建了一個簡單的POCO對象來表示一個「通知」:

public class Notification 
{ 
    public System.DateTime notificationDate; 

    public string notificationDetails; 

    public string status; 

    public string notificationTitle; 

    public string href; 
} 

我想讀取傳入的XML並創建一個對象列表。

List<Notification> notificationList; 

XElement x = XElement.Load(new StringReader(result)); 
if (x != null && x.Element("Notifications") != null) 
{ 
    notificationList = x.Element("Notifications") 
       .Elements("Notification") 
       .Select(e => new Notification() 
       .ToList(); 
} 

我真的不清楚'e'部分以及如何初始化新的Notification對象。你能幫忙嗎?

+2

多少經驗,你有LINQ?如果你是全新的LINQ教程,我建議你找一個好的LINQ教程。 – 2013-03-19 19:18:22

回答

5

e只是一個參數名稱您傳遞到lambda expressionnew Notification()。您可以使用它像這樣:

notificationList = x.Element("Notifications") 
        .Elements("Notification") 
        .Select(e => new Notification() 
           { 
            href = (string)e.Attribute("href") 
            notificationDetails = (DateTime)e.Element("NotificationDate") 
            notificationDate = (string)e.Element("NotificationDetails") 
            status = (string)e.Element("Status") 
            notificationTitle = (string)e.Element("NotificationTitle") 
           } 
        .ToList(); 

或者如果你喜歡的查詢語法

notificationList = 
    (from e in x.Element("Notifications").Elements("Notification") 
    select new Notification() 
      { 
       href = (string)e.Attribute("href") 
       notificationDetails = (DateTime)e.Element("NotificationDate") 
       notificationDate = (string)e.Element("NotificationDetails") 
       status = (string)e.Element("Status") 
       notificationTitle = (string)e.Element("NotificationTitle") 
      }) 
    .ToList(); 
1

使用object initialization語法:

notificationList = x.Element("Notifications") 
      .Elements("Notification") 
      .Select(e => new Notification() 
          { 
           href = (string)e.Attribute("href"), 
           notificationDate = (DateTime)e.Element("NotificationDate"), 
           notificationDetails = (string)e.Element("NotificationDetails"), 
           status = (string)e.Element("Status"), 
           notificationTitle = (string)e.Element("NotificationTitle") 
          }) 
      .ToList(); 

請記住,您可以輕鬆地投像XElementXAttribute對象爲stringintdoubleDateTime多。完整列表可以在這裏找到:XElement Type Conversions

0
notificationList = x.Element("Notifications") 
    .Elements("Notification") 
    .Select(e => new Notification() 
     { 
      notificationDate = DateTime.Parse(e.Element("NotificationDate").Value), 
      notificationDetails = e.Element("NotificationDetails").Value, 
      status = e.Element("Status").Value, 
      notificationTitle = e.Element("NotificationTitle").Value, 
      href = e.Attribute("href").Value 
     } 
    .ToList(); 
+0

您不必使用'DateTime.Parse',因爲定義了Explicit(XElement to DateTime)'。 – MarcinJuraszek 2013-03-19 19:20:57

+0

@MarcinJuraszek,謝謝。我不知道。我可以編輯答案,但它會和你的一樣。 :) – publicgk 2013-03-19 19:25:50