C# HtmlAlityPack属性?

C# HtmlAlityPack属性?,c#,html-agility-pack,C#,Html Agility Pack,我想做的就是 node.Attributes["class"].Value 但是如果节点没有class属性,它就会崩溃。所以,我必须先检查它是否存在,对吗?我该怎么做Attributes不是dict(它是一个包含内部dict的列表???),也没有HasAttribute方法(只有一个HasAttributes,指示它是否有任何属性)。我该怎么办?更新答案 如果缺少属性,则使用node.Attributes[“class”]?.Value返回null。这将与下面的值default()相同 原始答

我想做的就是

node.Attributes["class"].Value

但是如果节点没有
class
属性,它就会崩溃。所以,我必须先检查它是否存在,对吗?我该怎么做
Attributes
不是dict(它是一个包含内部dict的列表???),也没有HasAttribute方法(只有一个HasAttributes,指示它是否有任何属性)。我该怎么办?

更新答案

如果缺少属性,则使用
node.Attributes[“class”]?.Value
返回
null
。这将与下面的
值default()相同

原始答案

试试这个:

String val;
if(node.Attributes["class"] != null)
{
  val = node.Attributes["class"].Value;
}
或者你可以加上这个

public static class HtmlAgilityExtender
{
    public static String ValueOrDefault(this HtmlAttribute attr)
    {
        return (attr != null) ? attr.Value : String.Empty;
    }
}
然后使用

node.Attributes["class"].ValueOrDefault();
我还没有测试过这个,但它应该可以工作。

请尝试以下方法:

String abc = String.Empty;     
      if (tag.Attributes.Contains(@"type"))
      {
          abc = tag.Attributes[@"type"].Value;
      }

此代码可用于获取两个脚本标记之间的所有文本

String getURL(){
return @"some site address";
}
List<string> Internalscripts()
    {
        HtmlAgilityPack.HtmlDocument doc = new HtmlWeb().Load((@"" + getURL()));
        //Getting All the JavaScript in List
        HtmlNodeCollection javascripts = doc.DocumentNode.SelectNodes("//script");
        List<string> scriptTags = new List<string>();
        foreach (HtmlNode script in javascripts)
        {
            if(!script.Attributes.Contains(@"src"))
            {
                scriptTags.Add(script.InnerHtml);
            }
        }
        return scriptTags;
    }
String getURL(){
返回@“某个站点地址”;
}
列出内部脚本()
{
HtmlAgilityPack.HtmlDocument doc=new HtmlWeb().Load((@“+getURL());
//获取列表中的所有JavaScript
HtmlNodeCollection javascripts=doc.DocumentNode.SelectNodes(“//脚本”);
List scriptTags=new List();
foreach(javascripts中的HtmlNode脚本)
{
如果(!script.Attributes.Contains(@“src”))
{
scriptTags.Add(script.InnerHtml);
}
}
返回脚本标签;
}

HTML Agility Pack能够检查节点是否具有特定类或提供所有类的列表

    //Select nodes example, get all <p> tag nodes and check if they have a class and if they do, get the class attribute.
foreach (HtmlNode node in htmlAgilityDocument.DocumentNode.SelectNodes("//p"))
{
    if (node.HasClass("ClassName"))
    {
        HtmlAttribute classAttributes = node.Attributes["ClassName"];
        //Do something ...
    }
}

//Select nodes example, get all <p> tag nodes having a specified class name.
string className = "class";
foreach (HtmlNode node in htmlAgilityDocument.DocumentNode.SelectNodes("//p[@class='" + className + "']"))
{
    //Access via class attribute
    HtmlAttribute classAttribute = node.Attributes[className];
    //Do something ...
}

//Get all class names to check for a class
bool containsClass = htmlAgilityDocument.DocumentNode.GetClasses().Contains("ClassName");
if (containsClass == true)
{
    //Do something ...
}
//选择节点示例,获取所有标记节点并检查它们是否有类,如果有,则获取class属性。
foreach(HtmlAlityDocument.DocumentNode.SelectNodes(“//p”)中的HtmlNode节点)
{
if(node.HasClass(“ClassName”))
{
HtmlAttribute classAttributes=node.Attributes[“ClassName”];
//做点什么。。。
}
}
//选择节点示例,获取具有指定类名的所有标记节点。
字符串className=“class”;
foreach(HtmlAlityDocument.DocumentNode.SelectNodes(“//p[@class=”“+className+”])中的HtmlNode节点)
{
//通过类属性访问
HtmlAttribute classAttribute=node.Attributes[className];
//做点什么。。。
}
//获取所有类名以检查类
bool containsClass=htmlAgilityDocument.DocumentNode.GetClasses().Contains(“类名”);
if(containsClass==true)
{
//做点什么。。。
}

您确定检查
节点。属性[“类”]
不会返回空值吗?@Kirk:没错。。。。我认为它因为某种原因抛出了一个异常。很好的调用。希望他们能够实现类似val=node.Attributes[“class”]。Value???""; 如果您不关心任何地方的null…@Doggett,您可以创建一个
IfNotNull
扩展方法,该方法允许
node.Attribute[“class”].IfNotNull(x=>x.Value)
。不完全是你想要的,但我一直在用它来解决这类问题。@Kirk nice,从来没有想到过,只是注意到你可以使它完全通用,所以它适用于任何类型。。酷:)还是会喜欢这个???或者类似的东西;)实际上已经有一个
GetAttributeValue
方法。。。不需要再添加一个。这样就可以了:string classValue=node.Attributes[“className”]?.Value;