我希望能够将属性添加到html字符串,而无需构建解析器来处理html.在一个特定的情况下,我希望能够提取HTML的id或向html服务器端插入一个id.
说我有:
string stringofhtml = "<img src=\"someimage.png\" alt=\"the image\" />";
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这样的事情:
HtmlGenericControl htmlcontrol = new HtmlGenericControl(stringofhtml);
htmlcontrol.Attributes["id'] = "newid";
Run Code Online (Sandbox Code Playgroud)
要么
int theid = htmlcontrol.Attributes["id"];
Run Code Online (Sandbox Code Playgroud)
这只是我可以访问/添加我拥有的html字符串属性的一种方式.
你可以这样做:
HtmlGenericControl ctrl = new HtmlGenericControl();
ctrl.InnerHtml = "<img src=\"someimage.png\" alt=\"the image\" />";
Run Code Online (Sandbox Code Playgroud)
您也可以使用LiteralControl,而不是HtmlGenericControl:
LiteralControl lit = new LiteralControl(stringOfHtml);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9427 次 |
| 最近记录: |