开发者

Completing HTML tags

开发者 https://www.devze.com 2023-04-12 04:47 出处:网络
I am building a page using string builder methods etc. I have a scenario where I have to keep the html but space is limited and I have to cut the string to make it fit.

I am building a page using string builder methods etc.

I have a scenario where I have to keep the html but space is limited and I have to cut the string to make it fit.

The problem is that when cutting the string some HTML tags will have opening tags but no closing tags... and will cause page to break.

Is there some function of reading the string and adding the closing开发者_开发问答 tags relative to the content.

This is my current function:

public string cutTxtChkItalics(string val, int maxValue, string addToEnd)
{
    string rVal = val;

    if (val.Length >= maxValue)
        rVal = rVal.Substring(0, maxValue) + addToEnd;

    if (rVal.ToLower().Contains("<i>") && !rVal.ToLower().Contains("</i>"))
        rVal += "</i>";
    if (rVal.ToLower().Contains("<em>") && !rVal.ToLower().Contains("</em>"))
        rVal += "</em>";

    return rVal;
}


Hi you can use Html Agility pack.Here is some code sample i hope it help you. page content is the html code which is having some problem and SW is the stringWriter on which i am saving the corrected html.

 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(pagecontent);
        if (doc == null) ;
        doc.OptionCheckSyntax = true;
        doc.OptionAutoCloseOnEnd = true;
        doc.OptionFixNestedTags = true;
        int errorCount = doc.ParseErrors.Count();
        string output = "";
        doc.Save(SW);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号