gpt4 book ai didi

c# - 从 xhtml 文档中删除未关闭的开始

标签

转载 作者:太空狗 更新时间:2023-10-29 15:58:19 25 4
gpt4 key购买 nike

我有一个包含很多标签的大型 xhtml 文档。我观察到一些未闭合的开头段落标记不必要地重复,我想删除它们或用空格替换它们。我只想编写代码来识别未闭合的段落标签并删除它们。

这里有一个小例子来说明我的意思:

<p><strong>Company Registration No.1</strong> </p>
<p><strong>Company Registration No.2</strong></p>

<p> <!-- extra tag -->
<p> <!-- extra tag -->

<hr/>

<p><strong> HALL WOOD (LEEDS) LIMITED</strong><br/></p>
<p><strong>REPORT AND FINANCIAL STATEMENTS </strong></p>

谁能给我控制台应用程序的代码,只是为了删除这些未关闭的段落标签。

最佳答案

这应该有效:

public static class XHTMLCleanerUpperThingy
{
private const string p = "<p>";
private const string closingp = "</p>";

public static string CleanUpXHTML(string xhtml)
{
StringBuilder builder = new StringBuilder(xhtml);
for (int idx = 0; idx < xhtml.Length; idx++)
{
int current;
if ((current = xhtml.IndexOf(p, idx)) != -1)
{
int idxofnext = xhtml.IndexOf(p, current + p.Length);
int idxofclose = xhtml.IndexOf(closingp, current);

// if there is a next <p> tag
if (idxofnext > 0)
{
// if the next closing tag is farther than the next <p> tag
if (idxofnext < idxofclose)
{
for (int j = 0; j < p.Length; j++)
{
builder[current + j] = ' ';
}
}
}
// if there is not a final closing tag
else if (idxofclose < 0)
{
for (int j = 0; j < p.Length; j++)
{
builder[current + j] = ' ';
}
}
}
}

return builder.ToString();
}
}

我已经用您的示例对其进行了测试并且它有效...虽然它对于算法来说是一个糟糕的公式,但它应该为您提供一个入门基础!

关于c# - 从 xhtml 文档中删除未关闭的开始 <p> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3658403/

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