使用C#htmlagilitypack获得带有html的文本的较短预览版本

使用C#htmlagilitypack获得带有html的文本的较短预览版本,c#,html-agility-pack,C#,Html Agility Pack,我想用htmlagilitypack完成的事情是在保留html的同时制作内容的较短版本。有点像预览使用 例如,假设我有“123456789001234567890”,但长度设置为最大11。这将给出12345678901的结果 我现在不知道怎么去那里,因为仍然有效的html应该被保留,并且在检查包含html内容的文本的最大长度时被忽略。正则表达式对此不是有效的解决方案。 有什么想法吗?您可以使用此方法返回一个新的HtmlAgilityPack.HtmlDocument: public static

我想用htmlagilitypack完成的事情是在保留html的同时制作内容的较短版本。有点像预览使用

例如,假设我有
“123456789001234567890”
,但长度设置为最大11。这将给出
12345678901
的结果

我现在不知道怎么去那里,因为仍然有效的html应该被保留,并且在检查包含html内容的文本的最大长度时被忽略。正则表达式对此不是有效的解决方案。
有什么想法吗?

您可以使用此方法返回一个新的
HtmlAgilityPack.HtmlDocument

public static HtmlAgilityPack.HtmlDocument GetPreview(HtmlAgilityPack.HtmlDocument orginal, int maxTextLength)
{
    var docPreview = new HtmlAgilityPack.HtmlDocument();
    docPreview.DocumentNode.CopyFrom(orginal.DocumentNode, false); // documentation bug in HtmlAgilityPack, false means deep-copy

    string allText = docPreview.DocumentNode.InnerText;
    int remainingDelete = allText.Length - maxTextLength;

    if (remainingDelete <= 0)
        return docPreview;  // you are finished

    // select only text nodes
    HtmlNodeCollection allTextNodes = docPreview.DocumentNode.SelectNodes("//text()[normalize-space(.) != '']");
    // iterate text nodes backwards
    for (int i = allTextNodes.Count - 1; i >= 0; i--)
    {
        HtmlTextNode textNode = allTextNodes[i] as HtmlTextNode;
        if (textNode == null) continue;

        int length = remainingDelete >= textNode.Text.Length ? 0 : textNode.Text.Length - remainingDelete;
        int removeLetterCount = textNode.Text.Length - length;
        remainingDelete = remainingDelete - removeLetterCount;

        textNode.Text = textNode.Text.Substring(0, length);

        if (remainingDelete == 0)
            break;
    }

    return docPreview;
}
public静态HtmlAgilityPack.HtmlDocument GetPreview(HtmlAgilityPack.HtmlDocument-original,int-maxTextLength)
{
var docPreview=new HtmlAgilityPack.HtmlDocument();
docPreview.DocumentNode.CopyFrom(original.DocumentNode,false);//HtmlAgilityPack中的文档错误,false表示深度复制
字符串allText=docPreview.DocumentNode.InnerText;
int remainingDelete=allText.Length-maxTextLength;
如果(remainingDelete=0;i--)
{
HtmlTextNode textNode=allTextNodes[i]作为HtmlTextNode;
如果(textNode==null)继续;
int length=remainingDelete>=textNode.Text.length?0:textNode.Text.length-remainingDelete;
int removeLetterCount=textNode.Text.Length—长度;
remainingDelete=remainingDelete-removeLetterCount;
textNode.Text=textNode.Text.Substring(0,长度);
if(remainingDelete==0)
打破
}
返回文档预览;
}
您的样本:

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml("1234567890<div></div>1234567890");

HtmlAgilityPack.HtmlDocument docPreview = GetPreview(doc, 11);
Console.WriteLine(docPreview.DocumentNode.InnerHtml); // 1234567890<div></div>1
var doc=new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(“123456789001234567890”);
HtmlAgilityPack.htmldocumentdocpreview=GetPreview(doc,11);
Console.WriteLine(docPreview.DocumentNode.InnerHtml);//12345678901

你能分享你的代码吗?你试过什么?