2

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
  • 2
    How much experience do you have with LINQ? I'd suggest finding a good LINQ tutorial if you're completely new to it. Commented Mar 19, 2013 at 19:18

3 Answers 3

6

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();
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

2 Comments

You don't have to use DateTime.Parse because Explicit(XElement to DateTime) is defined.
@MarcinJuraszek, thanks. I wasn't aware. I can edit the answer but then it would be same as yours. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.