【问题标题】:How to read text present in html node in c#?如何在 C# 中读取 html 节点中存在的文本?
【发布时间】:2017-03-14 13:23:00
【问题描述】:

我想读取出现在 body 标签之间的数据(字符串),如下所示 -

<body> Text to read </body>

如何在不使用 HtmlAgilityPack 的情况下做到这一点?请帮忙。谢谢。

【问题讨论】:

  • @Ehsan Sajjad,没有其他办法吗?
  • 将文本解析为 XML 怎么样?
  • @hbulens ,我已经完成了 html 文件,包括内联 css、脚本标签等。所以这样做会有点困难。
  • 你可以使用body的InnerText
  • @VinothRaj ,请提供可能的解决方案。

标签: c# html regex asp.net-mvc


【解决方案1】:

我有解决方案,但没有使用 HtmlAgilityPack 和字符串函数。我使用了Regex。如下-

        string html = "<body>Text to read </body>";

        RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Singleline;
        Regex regx = new Regex("<body>(?<theBody>.*)</body>", options);

        Match match = regx.Match(html);

        if (match.Success)
        {
            string theBody = match.Groups["theBody"].Value;
            //Here I'm getting all the data present in body tag              

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多