简体   繁体   English

带有AngleSharp的HTML解析器 - IElement中的文本

[英]HTML Parser with AngleSharp - Text in IElement

I am writing a HTML parser with AngleSharp which should input HTML like this: 我正在编写一个带有AngleSharp的HTML解析器,它应该像这样输入HTML:

<p>
Paragraph Text
<a href="https://www.example com" class="external text" target="_new" rel="nofollow">Link Text</a>
Paragraph Text 2
</p>

and output it like this: 并输出如下:

<p>
Paragraph Text
<a href="https://www.example com">Link Text</a>
Paragraph Text 2
</p>

I wrote this recursive function to go through the whole document: 我编写了这个递归函数来遍历整个文档:

using AngleSharp.Dom;
using AngleSharp.Dom.Html;
using AngleSharp.Extensions;
using AngleSharp.Parser.Html;

private void processHTMLNode(IElement node, IElement targetNode)
{
    switch (node.NodeName.ToLower())
    {
    //...
    case "a":
        if(node.HasAttribute("href") && node.GetAttribute("href").StartsWith("#"))
        {
            break;
        }
        var aNew = outputDocument.CreateElement("a");
        aNew.SetAttribute("href", node.GetAttribute("href"));
        aNew.TextContent = node.TextContent;
        targetNode.AppendChild(aNew);
        break;
    case "p":
        var pNew = outputDocument.CreateElement<IHtmlParagraphElement>();
        foreach (var childNode in node.Children)
        {
            processHTMLNode(childNode, pNew);
        }
        //TODO fix this
        pNew.TextContent = node.TextContent;
        targetNode.AppendChild(pNew);
        break;
    }
    //...
}

The problem is, that setting the TextContent Attribute overwrites the a -Elements which are children of the p -Node. 问题是,在设定TextContent属性将覆盖a -elements这是儿童p -Node。 Also the order (text -> link -> text) is lost. 订单(文本 - >链接 - >文本)也会丢失。

How do i properly implement this? 我该如何正确实现这个?

Okay, so i managed to solve my problem using the following code: 好的,所以我设法使用以下代码解决了我的问题:

using AngleSharp.Dom;
using AngleSharp.Dom.Html;
using AngleSharp.Extensions;
using AngleSharp.Parser.Html;

private void processHTMLNode(INode node, IElement targetElement)
{
    IElement elementNode;
    IText textNode;

    if ((elementNode = node as IElement) != null)
    {
        switch (node.NodeName.ToLower())
        {
            //...
            case "a":
                if(node.HasAttribute("href") && node.GetAttribute("href").StartsWith("#"))
                {
                    break;
                }
                var aNew = outputDocument.CreateElement("a");
                aNew.SetAttribute("href", node.GetAttribute("href"));
                foreach (var childNode in elementNode.ChildNodes)
                {
                    processHTMLNode(childNode, aNew);
                }
                targetElement.AppendChild(aNew);
                break;
            case "p":
                var pNew = outputDocument.CreateElement("p");
                foreach (var childNode in node.Children)
                {
                    processHTMLNode(childNode, pNew);
                }
                targetElement.AppendChild(pNew);
                break;
            //...
        }

    }
    else if ((textNode = node as IText) != null)
    {
        var newTextNode = outputDocument.CreateTextNode(textNode.Text);
        targetElement.AppendChild(newTextNode);
    }
}

This image from the AngleSharp Documentation helped me a lot: AngleSharp DOM AngleSharp文档中的这张图片帮了我很多: AngleSharp DOM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM