HtmlAgilityPack.HtmlNode.SetAttributeValue C# (CSharp) Method

SetAttributeValue() public method

Helper method to set the value of an attribute of this node. If the attribute is not found, it will be created automatically.
public SetAttributeValue ( string name, string value ) : HtmlAgilityPack.HtmlAttribute
name string The name of the attribute to set. May not be null.
value string The value for the attribute.
return HtmlAgilityPack.HtmlAttribute
		public HtmlAttribute SetAttributeValue(string name, string value)
		{
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}
			HtmlAttribute att = Attributes[name];
			if (att == null)
			{
				return Attributes.Append(_ownerdocument.CreateAttribute(name, value));
			}
			att.Value = value;
			return att;
		}

Usage Example

Ejemplo n.º 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::SetAttributeValue