HtmlAgilityPack HasAttribute?
All I want to do is
node.Attributes["class"].Value
But if the node doesn't have the class
attribute, it crashes. So, I have to check for its existence first, right? How do I do that? Attributes
is not a dict (its a list tha开发者_Python百科t contains an internal dict??), and there's no HasAttribute method (just a HasAttributes which indicates if it has any attribute at all). What do I do?
Updated answer
Use node.Attributes["class"]?.Value
to return null
if the attribute is missing. This will be the same as the ValueOrDefault()
below.
Original answer
Try this:
String val;
if(node.Attributes["class"] != null)
{
val = node.Attributes["class"].Value;
}
Or you might be able to add this
public static class HtmlAgilityExtender
{
public static String ValueOrDefault(this HtmlAttribute attr)
{
return (attr != null) ? attr.Value : String.Empty;
}
}
And then use
node.Attributes["class"].ValueOrDefault();
I havent tested that one, but it should work.
Please try this:
String abc = String.Empty;
if (tag.Attributes.Contains(@"type"))
{
abc = tag.Attributes[@"type"].Value;
}
This Code Can Be Used to get all text between two script tags.
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;
}
HTML Agility Pack has the capability to check if a node has a particular class or provide a list of all classes.
//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 ...
}
精彩评论