Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find an XElement with a certain attribute name and value with LINQ

XDocument xDocument = XDocument.Load("...");
IEnumerable<XElement> elements = xDocument
    .Element("lfm")
    .Element("events")
    .Elements("event");

try
{            
    foreach (XElement elm in elements)
    {
        comm.Parameters.AddWithValue("extID", elm.Element("id").Value  ?? "");
        comm.Parameters.AddWithValue("Title", elm.Element("title").Value ?? "");
        comm.Parameters.AddWithValue("HeadlineArtist", 
        elm.Element("artists").Element("headliner").Value ?? "");

but I want the value of the element "image" with the attribute "size=large", I have been looking all night, and this is the closest I have come:

comm.Parameters.AddWithValue("LargeImage",
    elm.Descendants("image")
       .FirstOrDefault(i => (string)i.Attribute("size") == "large").Value);

Sample of the part of XML response:

<lfm status="ok">
    <events xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
            location="Chicago, United States" page="1" perPage="1"
            totalPages="341" total="341" festivalsonly="0" tag="">
        <event xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
            <id>3264699</id>
            <title>Iron And Wine</title>
            <artists>
                <artist>Iron And Wine</artist>
                <artist>Dr. John</artist>
                <headliner>Iron And Wine</headliner>
            </artists>
            <venue>
                <id>8915382</id>
                <name>Ravinia Festival</name>
                <location>
                    <city>Highland Park</city>
                    <country>United States</country>
                    <street>200 Ravinia Park Rd</street>
                    <postalcode>60035</postalcode>
                    <geo:point>
                        <geo:lat>42.15831</geo:lat>
                        <geo:long>-87.778409</geo:long>
                    </geo:point>
                </location>
                <url>http://www.last.fm/venue/8915382+Ravinia+Festival</url>
                <website>http://www.ravinia.org/</website>
                <phonenumber>847.266.5100</phonenumber>
                <image size="small">http://userserve-ak.last.fm/serve/34/63026487.jpg</image>
                <image size="medium">http://userserve-ak.last.fm/serve/64/63026487.jpg</image>
                <image size="large">http://userserve-ak.last.fm/serve/126/63026487.jpg</image>
                <image size="extralarge">http://userserve-ak.last.fm/serve/252/63026487.jpg</image>
like image 564
Scott Selby Avatar asked Jun 11 '12 14:06

Scott Selby


People also ask

How to get XElement attribute value in c#?

If you are getting the value and the attribute might not exist, it is more convenient to use the explicit conversion operators, and assign the attribute to a nullable type such as string or Nullable<T> of Int32 . If the attribute does not exist, then the nullable type is set to null.

How to get XML attribute value in c#?

You can access the value for any attribute using the default collection on the xml reader. see the code xr["i"] just replace the "i" with any attribute name.

What is XElement C#?

The XElement class is one of the fundamental classes in LINQ to XML. It represents an XML element. The following list shows what you can use this class for: Create elements. Change the content of the element.


1 Answers

Try

XElement result = elm.Descendants("image")
   .FirstOrDefault(el => el.Attribute("size") != null &&
                         el.Attribute("size").Value == "large");
if (result != null) {
    process result.Value ...
}

Starting with C#6.0 (VS 2015), you can write:

XElement result = elm.Descendants("image")
   .FirstOrDefault(el => el.Attribute("size")?.Value == "large");
if (result != null) {
    process result.Value ...
}

A non-obvious alternative (as @RandRandom pointed out) is to cast the Attribute to string:

XElement result = elm.Descendants("image")
   .FirstOrDefault(el => (string)el.Attribute("size") == "large");
if (result != null) {
    process result.Value ...
}

This works, because because of XAttribute Explicit Conversion (XAttribute to String).

like image 184
Olivier Jacot-Descombes Avatar answered Oct 19 '22 15:10

Olivier Jacot-Descombes