简体   繁体   English

从标签Linq到XML获取属性值

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

I'm using Linq to get some xml values, but this time I want to get the content of "href" attribute from tag, which is like this: 我正在使用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"/>

any clues how to do that? 任何线索该怎么做?

I am able to get values of tags 我可以获取标签的值

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

this way: 这条路:

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

But I failed to retrieve a value from the href attribute, any help is appreciated. 但是我无法从href属性中检索值,我们将提供任何帮助。

How about this: 这个怎么样:

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

... or just: ... 要不就:

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