HtmlAgilityPack.HtmlNode.GetAttributeValue C# (CSharp) Method

GetAttributeValue() public method

Helper method to get the value of an attribute of this node. If the attribute is not found, the default value will be returned.
public GetAttributeValue ( string name, string def ) : string
name string The name of the attribute to get. May not be null.
def string The default value to return if not found.
return string
		public string GetAttributeValue(string name, string def)
		{
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}

			if (!HasAttributes)
			{
				return def;
			}
			HtmlAttribute att = Attributes[name];
			if (att == null)
			{
				return def;
			}
			return att.Value;
		}

Same methods

HtmlNode::GetAttributeValue ( string name, bool def ) : bool
HtmlNode::GetAttributeValue ( string name, int def ) : int

Usage Example

Beispiel #1
1
        public bool SetValue(HtmlNode n, string value)
        {
            if (n is HtmlNode && n.Name == "select")
            {
                foreach (HtmlNode o in n.SelectNodes("option"))
                {
                    o.SetAttributeValue("selected", o.GetAttributeValue("value", "").Equals(value) ? "selected" : "");
                }
                return true;
            }

            if (n is HtmlNode && n.Name == "input")
            {
                switch (n.GetAttributeValue("type", ""))
                {
                    case "radio":
                        n.SetAttributeValue("checked", n.GetAttributeValue("value", "").Equals(value) ? "checked" : "");
                        break;
                    default:
                        n.SetAttributeValue("value", value);
                        break;
                }
                n.SetAttributeValue("value", value);
                return true;
            }

            return false;
        }
All Usage Examples Of HtmlAgilityPack.HtmlNode::GetAttributeValue