gpt4 book ai didi

c# - 从字符串格式化 Html 代码

转载 作者:可可西里 更新时间:2023-11-01 13:12:35 25 4
gpt4 key购买 nike

我基本上有一个来自一些 html 代码数据库的字符串。现在我想在 asp.net 网页上的某种文本区域中显示 html 代码,用户可以从那里编辑 html 代码,其中包括缩进等......并且用户可以将 html 代码保存回数据库。

我读过有关使用非托管代码的 TidyManaged 的​​资料,但我不想使用非托管代码。

是否有一些示例代码或网站将其解释为格式化 html 代码,如 - Html Formatter

任何指导或帮助将不胜感激。

编辑:

我只想缩进 html 标签,记住解析的 html 字符串可能不符合要求,这对这个应用程序来说很好。

使用的 html 字符串示例 -

string a = "<html><body><h1>hello</h1></body></html>

输出应该如下所示

<html>
<body>
<h1>
hello
</h1>
</body>
</html>

最佳答案

这很长,但我认为它可以满足基本需求。我决定自己动手而不是使用库......也许不是最有效的代码:)

    /// <summary>
/// This function creates an indented format of HTML with new lines for all elements and text. Errors result in the original text being returned.
/// </summary>
private static string FormatHtml(string content)
{
string original = content;
string open = "<";
string slash = "/";
string close = ">";

int depth = 0; // the indentation
int adjustment = 0; //adjustment to depth, done after writing text

int o = 0; // open < index of this character
int c = 0; // close > index of this character
int s = 0; // slash / index of this character
int n = 0; // next where to start looking for characters in the next iteration
int b = 0; // begin resolved start of usable text
int e = 0; // end resolved end of usable test

string snippet;

try
{
using (StringWriter writer = new StringWriter())
{
while (b > -1 && n > -1)
{
o = content.IndexOf(open, n);
s = content.IndexOf(slash, n);
c = content.IndexOf(close, n);
adjustment = 0;

b = n; // begin where we left off in the last iteration
if (o > -1 && o < c && o == n)
{
// starts with "<tag>text"
e = c; // end at the next closing tag
adjustment = 2; //for after this node
}
else
{
// starts with "text<tag>"
e = o - 1; // end at the next opening tag
}

if (b == o && b + 1 == s) // ?Is the 2nd character a slash, this the a closing tag: </div>
{
depth -= 2;//adjust immediately, not afterward ...for closing tag
adjustment = 0;
}

if ((s + 1) == c && c == e) // don't adjust depth for singletons: <br/>
{
adjustment = 0;
}



//string traceStart = content.Substring(0, b);
int length = (e - b + 1);
if (length < 0)
{
snippet = content.Substring(b); // happens on the final iteration
}
else
{
snippet = content.Substring(b, (e - b + 1));
}
//string traceEnd = content.Substring(b);


if (snippet == "<br>" || snippet == "<hr>") // don't adjust depth for singletons which lack slashes: <br>
{
adjustment = 0;
}

//Write the text
if (!string.IsNullOrEmpty(snippet.Trim()))
{
//Debug.WriteLine(snippet);
writer.Write(Environment.NewLine);
if (depth > 0) writer.Write(new String(' ', depth)); // add the indentation
writer.Write(snippet);
}

depth += adjustment; //adjust for the next line which is likely nested

n = e + 1; // the next iteration start at the end of this one.

}

return writer.ToString();
}
}
catch (Exception ex)
{
Log("Unable to format html. " + ex.Message);
return original;
}
}

关于c# - 从字符串格式化 Html 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18077026/

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