如何在XPath中检查多个属性?

Ars*_*ray 7 c# xml xpath

我想在XHTML文档中选择样式表,它不仅包含描述,还包含href.

例如

<link rel="stylesheet" href="123"/> 
Run Code Online (Sandbox Code Playgroud)

应该选择,和

<link rel="stylesheet"/>  
Run Code Online (Sandbox Code Playgroud)

不应该.

目前,我这样做:

foreach (XmlNode n in xml.SelectNodes(@"//link[@rel='stylesheet']"))
{
    if (n.Attributes["href"]==null||n.Attributes[""].Value==null)
    {
        continue;
    }
    var l = Web.RelativeUrlToAbsoluteUrl(stuffLocation, n.Attributes["href"].Value);
}
Run Code Online (Sandbox Code Playgroud)

但我怀疑有更好的方法可以做到这一点.在那儿?

Bol*_*ock 7

添加and @href到属性表达式:

//link[@rel='stylesheet' and @href]

这应该允许你完全省略检查:

foreach (XmlNode n in xml.SelectNodes(@"//link[@rel='stylesheet' and @href]"))
{
    var l = Web.RelativeUrlToAbsoluteUrl(stuffLocation, n.Attributes["href"].Value);
}
Run Code Online (Sandbox Code Playgroud)