gpt4 book ai didi

c# - 需要 Body 标签内的数据,但不需要任何其他标签

转载 作者:行者123 更新时间:2023-11-28 02:49:35 25 4
gpt4 key购买 nike

您好,我有 html 格式的简历,我正在使用 StreamReader 读取文件,并且正在使用以下方法删除标签。

using (StreamReader sr = new StreamReader("\\Myfile.html"))
{
String line = sr.ReadToEnd();
string jj = Regex.Replace(line, "<.*?>", String.Empty);
}

它的工作该死的酷

但是根据我的要求,我只需要 body 标签内的数据。但没有 body 标签,里面也没有标签。

最佳答案

不要使用 Regex 进行 HTML/XML 解析。使用 Html/Xml 解析器。这里很好地解释了为什么你不应该使用它。

RegEx match open tags except XHTML self-contained tags

Can you provide some examples of why it is hard to parse XML and HTML with a regex?

您可以使用 HTML Agility pack 加载 Html 文档中的字符串

这里是如何做到这一点的小例子:

public string ReplacePElement() 
{
HtmlDocument doc = new HtmlDocument();
doc.Load(htmlFile);

foreach(HtmlNode p in doc.DocumentNode.SelectNodes("body"))
{

}

return doc.DocumentNode.OuterHtml;
}

关于c# - 需要 Body 标签内的数据,但不需要任何其他标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39924714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com