【问题标题】:How do I use LINQ to XML to create a list of objects?如何使用 LINQ to XML 创建对象列表?
【发布时间】:2013-03-20 03:25:58
【问题描述】:

我尝试使用 LINQ 读取一些入站 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 对象。你能帮忙吗?

【问题讨论】:

  • 您对 LINQ 有多少经验?如果您完全是新手,我建议您找一个好的 LINQ 教程。

标签: c# linq linq-to-xml


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用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

    【讨论】:

      【解决方案3】:
      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();
      

      【讨论】:

      • 您不必使用DateTime.Parse,因为Explicit(XElement to DateTime) 已定义。
      • @MarcinJuraszek,谢谢。我不知道。我可以编辑答案,但它会和你的一样。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多