代码改变世界

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

  音乐让我说  阅读(333)  评论(0编辑  收藏  举报

文档地址: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);
        }
    }
}
复制代码

 

谢谢浏览!

点击右上角即可分享
微信分享提示