使用LINQ查找具有特定属性名称和值的XElement

20
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 ?? "");

但是我想获取带有属性"size=large"的元素"image"的值,我已经查找了一整晚,以下是我能够接近的答案:

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

以下是XML响应的一部分示例:

<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>

那么,问题在哪里?那看起来很好,但这取决于elm是什么(您没有展示如何从xDocument到elm)。 - moribvndvs
当没有找到具有该属性的元素时,您将收到“NullReferenceException”。 - MarcinJuraszek
2个回答

37

试试

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

从C#6.0(VS 2015)开始,你可以这样写:

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

正如 @RandRandom 指出的那样,一个不太显而易见的替代方法是将 Attribute 强制转换为 string:

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

这行代码能够正常运行,是因为利用了XAttribute 显式转换 (XAttribute to String)


由于某种原因,一开始这个程序无法正常工作,但现在它表现得非常出色! - Scott Selby
2
你可以这样写,而不是使用 ?.Value 或 .Value 开始: (string)el.Attribute("size") == "large"(适用于所有版本) - Rand Random

18

你可以使用XPathSelectElement扩展方法

var node = elm.XPathSelectElement("descendant::image[@size='large']");
if (node!=null)
{
    var path = node.Value;
}

1
惊人的答案!已测试并适用于.NET Core/.NET Standard 1.6! - Steve Johnson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接