【问题标题】:How to remove the html tag and css in c# parameter如何去除c#参数中的html标签和css
【发布时间】:2019-07-20 20:31:28
【问题描述】:

C#代码:-

dynamic RetArgs = new JObject();
RetArgs.abcTest= System.Text.RegularExpressions.Regex.Replace(abcTest, @"<[^>]*(>|$)|&nbsp;|&zwnj;|&raquo;|&laquo;", string.Empty).Trim();

显示参数值:-

<div style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; line-height: inherit; font-family: Arimo; vertical-align: baseline; color: rgb(0, 0, 0);">
    Take your medicines as per following directions for 1 month/ 1 महिने के लिए</div>

预期结果:-

Take your medicines as per following directions for 1 month/ 1 महिने के लिए

【问题讨论】:

  • 我认为这里的正常建议是使用为解析 HTML 而设计的东西,例如 HtmlAgilityPack。您能否建议您当前的解决方案为何/如何在这种情况下不起作用?
  • @john 实际上这是我的 post 方法这个输入参数传递。并将此值插入 ck-editor 然后生成此值
  • @john 当前解决方案不起作用,但我只是尝试一下。
  • 在你尝试编写正则表达式来解析 html 之前,先看看这个RegEx match open tags except XHTML self-contained tags
  • 当我针对“显示参数值”示例对其进行测试时,当前的解决方案对我有用——我从Regex.Replace 中得到了“预期结果”。 “不工作”并不是对问题的准确描述,因为“不工作”涵盖了从抛出异常到返回空白字符串到返回仍然包含 HTML 的字符串等所有内容。

标签: javascript c# asp.net asp.net-mvc-4


【解决方案1】:

我建议使用HtmlAgilityPackNuGet 提供)

这段代码:

string html = "<div style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; line-height: inherit; font-family: Arimo; vertical-align: baseline; color: rgb(0, 0, 0);">
    Take your medicines as per following directions for 1 month/ 1 महिने के लिए</div>";
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(description);
var fixedHtml = htmlDoc.DocumentNode.InnerText;

产生这个输出:

Take your medicines as per following directions for 1 month/ 1 महिने के लिए

【讨论】:

    猜你喜欢
    • 2011-01-21
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 2012-06-06
    • 2014-06-18
    相关资源
    最近更新 更多