文档地址:https://anglesharp.github.io/docs/Examples.html

 

直接贴代码了:

using System;
using System.Linq;
using System.Threading.Tasks;
using AngleSharp;
using AngleSharp.Html.Parser;

namespace AngleSharpSamples
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var config = Configuration.Default.WithDefaultLoader();
            
            var context = BrowsingContext.New(config);

            //Source to be parsed
            var source = "<h1>Some example source</h1><p>This is a paragraph element";

            //Create a virtual request to specify the document to load (here from our fixed string)
            var document = await context.OpenAsync(req => req.Content(source));

            //Do something with document like the following
            Console.WriteLine("Serializing the (original) document:");
            Console.WriteLine(document.DocumentElement.OuterHtml);

            var p = document.CreateElement("p");
            p.TextContent = "This is another paragraph.";

            Console.WriteLine("Inserting another element in the body ...");
            document.Body.AppendChild(p);

            Console.WriteLine("Serializing the document again:");
            Console.WriteLine(document.DocumentElement.OuterHtml);
        }
    }
}

 

AngleSharp 实战(02)之传递 HTML 内容给 AngleSharp

谢谢浏览!

相关文章:

  • 2022-12-23
  • 2021-12-05
  • 2021-11-30
  • 2021-06-15
  • 2021-06-16
  • 2021-07-04
猜你喜欢
  • 2021-10-15
  • 2021-08-26
  • 2021-09-08
  • 2022-12-23
  • 2021-11-12
  • 2019-12-18
  • 2022-12-23
相关资源
相似解决方案