1

有没有办法让所有文本格式都带有<div></div>标签,而不会有任何未关闭的标签溢出到页面的其余部分?

我有一个repeater控件,可以将内容从 a显示database到. 为了减少空间成本,我将字符串格式化为1000 个字符。不幸的是,这会切断任何结束标记,并导致页面的其余部分生效。label<div>

我需要找到一种方法来呈现<div>'s last,或者强制标签关闭。
我不认为这htmlAgilityPack会起作用。

我不知道该怎么做,或者从哪里开始,所以我没有代码可以显示。谁能指出我正确的方向。

4

2 回答 2

2

Html Agility Pack确实可以自动关闭标签。例如,这段代码

string html = "<div>hello<b>bold<i>and italic";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
doc.Save(Console.Out);

会输出这个:

<div>hello<b>bold<i>and italic</i></b></div>
于 2013-04-29T10:36:34.980 回答
0

我创建了一种方法来检查字符串并关闭标签。我将更新代码的任何进一步建议。

    public string FormatClosingTags(string origionalText)
    {
        string manipulate = origionalText;

        // Get the tags away from the words.
        manipulate = manipulate.Replace(">", "> ");
        manipulate = manipulate.Replace("<", " <");

        // Now that the tags are alone and weak, split them up!
        string[] tags = manipulate.Split(' ');

        // Create holding cells to sibigate the tags.
        List<string> openingTags = new List<string>();
        List<string> closingTags = new List<string>();

        // Create a marshal to hold the subjugated tags.
        StringBuilder output = new StringBuilder();

        // Find all those tags!
        foreach (string s in tags)
        {
            // Make sure its only the women and children
            if ((s.Contains("<") || s.Contains(">")) && (!s.Contains("</")))
            {
                openingTags.Add(s);
            }
            // While keeping the males to themsleves
            else if ((s.Contains("<") || s.Contains(">")) && (s.Contains("</")))
            {
                closingTags.Add(s);
            }
        }


        // Get one of those harsh ladies with a clipboard and make her count all the men
        int counter = closingTags.Count;

        // Destroy all the females that have a male
        openingTags.RemoveRange(0, counter);

        // Find the rest of the lonely women
        foreach (string open in openingTags)
        {
            // CONVERT THEM TO MEN - add them to the marshal's list
            output.Append(open.Replace("<", "</"));
        }

        return origionalText + output;
    }
于 2013-04-29T10:29:01.670 回答