用c#格式化HTML字符串

本文关键字:字符串 HTML 格式化 | 更新日期: 2023-09-27 18:06:23

我正在寻找c#上http://prettydiff.com/的美化代码功能的模拟。例如,我有这个字符串:

<div><div><input type="radio" value="radio" id="radio_0">Radio</div><div><input type="radio" value="radio" id="radio_1">Radio</div><div><input type="radio" value="radio" id="radio_2">Radio</div><div><input type="checkbox" name="checkbox" value="checkbox" id="checkbox_3">Checkbox</div><div><p id="text_4">HELLO</p></div><div><p id="text_5">HELLO</p></div></div>

然后我想得到这样的东西:

<div>
     <div><input id="radio_0" type="radio" value="radio">Radio</div>
     <div><input id="radio_1" type="radio" value="radio">Radio</div>
     <div><input id="radio_2" type="radio" value="radio">Radio</div>
     <div><input id="checkbox_3" name="checkbox" type="checkbox" value="checkbox">Checkbox</div>
     <div><p id="text_4">HELLO</p></div>
     <div><p id="text_5">HELLO</p></div>

如果你知道可以帮助我的工具或有任何想法,请写下来:)Ty.

用c#格式化HTML字符串

尝试任何HTML-to-XHTML解析器,如htmllagilitypack、SgmlReader等

另一个解决方案是resharper

我正在使用它,我也为自己定制了它

如果您使用的是Visual Studio,您可以按Ctrl + KCtrl + D

你也可以在菜单中的"编辑>高级>格式化文档"中找到它。

编辑

我刚刚注意到,在VS 2010, VS 2008和VS 2005的菜单选项是Ctrl + ECtrl + D。我可以在旧版本中使用Ctrl + KCtrl + D,但我不能在VS 2013中使用Ctrl + ECtrl + D

我在这台机器上没有VS 2012,所以我无法确认此时是否Ctrl + ECtrl + D工作,但我一直使用Ctrl + KCtrl + D在过去。

如果您不喜欢默认设置,这里是关于如何自定义按钮的附加信息。