简体   繁体   English

简洁的LINQ to XML查询

[英]Succinct LINQ to XML Query

Assuming you have the following XML: 假设您有以下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? 使用LINQ to XML,获得给定类型图像的System.Uri最简洁,最健壮的方法是什么? 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. 特别是当您考虑XPath等效时。

Can anyone point out a better way? 任何人都能指出更好的方法吗?

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 :) 给那个去吧:)

return xml.XPathSelectElements(string.Format("//image[info/imageType='{0}']/imagedata/@fileref",imageType))
.Select(u=>new Uri(u.Value)).FirstOrDefault();

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): 如果null检查是优先级(它似乎是):

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. 不确定你是否会比使用null检查更简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM