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 开发者_C百科&& 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?
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):
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.
精彩评论