gpt4 book ai didi

c# - 如何检查 XPath 中的多个属性?

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:46 27 4
gpt4 key购买 nike

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

例如

<link rel="stylesheet" href="123"/> 

应该被选中,并且

<link rel="stylesheet"/>  

不应该。

目前我是这样做的:

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);
}

但我怀疑有更好的方法来做到这一点。有吗?

最佳答案

在属性表达式中添加和@href:

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

This should allow you to omit the check altogether:

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

关于c# - 如何检查 XPath 中的多个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com