Johnny Westlake
Johnny Westlake

Reputation: 1459

XML Linq Newbie Question

I'm working with a XML file that looks something like this:

<lfm status="ok">
    <user>
       <name>JohnnyWestlake</name>
       <image size="large">http://someurl.com/large.jpg</image>
       <image size="extralarge">ttp://someurl.com/exlarge.jpg</image>
       ...
    </user>
</lfm>

And I'm adding this to a user class using Linq like so:

        User user;

        user = (from lfmUser in userrequest.Descendants("user")
               select new User
               {
                   Name = lfmUser.Element("name").Value,
                   ImageM = lfmUser.Element("image").Value,
                   ...
               }).FirstOrDefault();

Question, how can I set ImageM to the url contained in image size="extralarge", and not image size="large"? Or should I go about it another way?

Upvotes: 2

Views: 105

Answers (2)

Rob
Rob

Reputation: 45771

Try this:

var user = (from lfmUser in userrequest.Descendants("user")
    select new User
    {
        Name = lfmUser.Element("name").Value,
        ImageM = lfmUser.Descendants("image").Where(x=>x.Attribute("size").Value == "large").First().Value
    }).FirstOrDefault();

Upvotes: 0

&#181;Bio
&#181;Bio

Reputation: 10748

ImageM = lfmUser.Elements("image")
                .Where( e => e.Attribute( "size" ).Value == "extralarge" )

Upvotes: 3

Related Questions