我使用C#并需要解析HTML以将属性读入键值对.例如,给出以下HTML片段
<DIV myAttribute style="BORDER-BOTTOM: medium none; BACKGROUND-COLOR: transparent; BORDER-TOP: medium none" id=my_ID anotherAttribNamedDIV class="someclass">
Run Code Online (Sandbox Code Playgroud)
请注意,属性可以是
1.key ="value"对,例如class="someclass"
2. key = value pair,例如id=my_ID(没有值的引号)
3.普通属性,例如myAttribute,没有"值"
我需要将它们存储到具有键值对的字典中,如下所示
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"
我正在寻找正则表达式来做到这一点.
Mik*_*keM 10
您可以使用HtmlAgilityPack执行此操作
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 />";
}
Run Code Online (Sandbox Code Playgroud)