繁体   English   中英

从标签Linq到XML获取属性值

[英]Get attribute value from a tag Linq to XML

我正在使用Linq获取一些xml值,但是这次我想从标记中获取“ href”属性的内容,如下所示:

<link rel="alternate" type="text/html" href="Value I want to retrieve"/>
<link rel="alternate" type="text/html" href="Another Value want to retrieve"/>

任何线索该怎么做?

我可以获取标签的值

<title>1st title</title>
<title>2nd title</title>

这条路:

IEnumerable<XElement> item = document.Descendants(xmlns + "title");
// to print use: item.ElementAt<XElement>(0).Value;

但是我无法从href属性中检索值,我们将提供任何帮助。

这个怎么样:

IEnumerable<string> links = document.Descendants("link")
    .Select(element => element.Attribute("href").Value);

... 要不就:

var links = document.Descendants("link")
    .Attributes("href")
    .Select(element => element.Value);

暂无
暂无

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

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