How do I use LINQ to XML to create a list of objec

2019-05-29 03:32发布

问题:

I have some inbound XML that I am trying to read with LINQ to generate a list of objects:

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

And I've created a simple POCO object to represent a 'Notification':

public class Notification
{
    public System.DateTime notificationDate;

    public string notificationDetails;

    public string status;

    public string notificationTitle;

    public string href;
}

and I'd like to read the incoming XML and create a list of objects.

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

I'm really unclear about the 'e' part and how to initialize a new Notification object. Can ya help?

回答1:

e is just a parameter name your passing to the lambda expression, new Notification(). You can use it like this:

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

Or if you prefer query syntax

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:

Use object initialization syntax:

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

Remember, that you can easily cast objects like XElement and XAttribute into string, int, double, DateTime and more. Complete list can be found here: 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();