c# - How to parse an HTML node's attributes -
i use c# , need parse html read attributes key value pairs. e.g given following html snippet
<div myattribute style="border-bottom: medium none; background-color: transparent; border-top: medium none" id=my_id anotherattribnameddiv class="someclass"> please note attributes can be
1. key="value" pairs e.g class="someclass"
2. key=value pairs e.g id=my_id (no quotes values)
3. plain attributes e.g myattribute, doesn't have "value"
i need store them dictionary key value pairs follows
key=myattribute value=""
key=style value="border-bottom: medium none; background-color: transparent; border-top: medium none"
key=id value="my_id"
key=anotherattribnameddiv value=""
key=class value="someclass"
i looking regular expressions this.
you can htmlagilitypack on codeplex
string mydiv = @"<div myattribute style=""border-bottom: medium none; background-color: transparent; border-top: medium none"" id=my_id anotherattribnameddiv class=""someclass""></div>"; htmldocument doc = new htmldocument(); doc.loadhtml(mydiv); htmlnode node = doc.documentnode.selectsinglenode("div"); literal1.text = ""; foreach (htmlattribute attr in node.attributes) { literal1.text += attr.name + ": " + attr.value + "<br />"; }
Comments
Post a Comment