tags:

views:

55

answers:

3

Assuming you have the following XML:

<?xml version="1.0" encoding="utf-8"?>

<content>
    <info>
        <media>
            <image>
                <info>
                    <imageType>product</imageType>
                </info>
                <imagedata fileref="http://www.example.com/image1.jpg" />
            </image>
            <image>
                <info>
                    <imageType>manufacturer</imageType>
                </info>
                <imagedata fileref="http://www.example.com/image2.jpg" />
            </image>
        </media>
    </info>
</content>

Using LINQ to XML, what is the most succinct, robust way to obtain a System.Uri for an image of a given type? At the moment I have this:

private static Uri GetImageUri(XElement xml, string imageType)
{
    return (from imageTypeElement in xml.Descendants("imageType")
            where imageTypeElement.Value == imageType && imageTypeElement.Parent != null && imageTypeElement.Parent.Parent != null
            from imageDataElement in imageTypeElement.Parent.Parent.Descendants("imagedata")
            let fileRefAttribute = imageDataElement.Attribute("fileref")
            where fileRefAttribute != null && !string.IsNullOrEmpty(fileRefAttribute.Value)
            select new Uri(fileRefAttribute.Value)).FirstOrDefault();
}

This works, but feels way too complicated. Especially when you consider the XPath equivalent.

Can anyone point out a better way?

+1  A: 
var images = xml.Descentants("image");

return images.Where(i => i.Descendants("imageType")
                          .All(c => c.Value == imageType))
             .Select(i => i.Descendants("imagedata")
                           .Select(id => id.Attribute("fileref"))
                           .FirstOrDefault())
             .FirstOrDefault();

Give that a go :)

Aren
+1 Thanks, but it's still way more verbose than the XPath equivalent...
Kent Boogaart
+1  A: 
return xml.XPathSelectElements(string.Format("//image[info/imageType='{0}']/imagedata/@fileref",imageType))
.Select(u=>new Uri(u.Value)).FirstOrDefault();
Gregoire
Maybe I should have said more explicitly: "without using XPath". I'm well aware that the XPath is more succinct, and needed some convincing not to switch to it. Thanks though.
Kent Boogaart
@Kent Boogaart : sorry I had misunderstood your question
Gregoire
A: 

If you can guarantee the file will always have the relevant data, then with no type checking:

private static Uri GetImageUri(XElement xml, string imageType)
{
    return (from i in xml.Descendants("image")
            where i.Descendants("imageType").First().Value == imageType
            select new Uri(i.Descendants("imagedata").Attribute("fileref").Value)).FirstOrDefault();
}

If null checking is a priority (and it seems it is):

private static Uri GetSafeImageUri(XElement xml, string imageType)
{
    return (from i in xml.Descendants("imagedata")
            let type = i.Parent.Descendants("imageType").FirstOrDefault()
            where type != null && type.Value == imageType
            let attr = i.Attribute("fileref")
            select new Uri(attr.Value)).FirstOrDefault();
}

Not sure if you'll get much more succinct than that with null checking.

jeffora