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