C#AngleSharp解析流有什么问题?

C#AngleSharp解析流有什么问题?,c#,asynchronous,anglesharp,C#,Asynchronous,Anglesharp,问题是,文档中只写入了一部分,而不是所需数量的标题。控制台输出正常 此处代码: using AngleSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AngleSharp { class Program { static void Main(s

问题是,文档中只写入了一部分,而不是所需数量的标题。控制台输出正常

此处代码:

using AngleSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace AngleSharp

{
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("ok start");
             Mainpar();
             Console.ReadKey();
        }
    static async Task Mainpar()
    {
        // Setup the configuration to support document loading
        var config = Configuration.Default.WithDefaultLoader();
        // Load the names of all The Big Bang Theory episodes from Wikipedia
        var address = "https://en.wikipedia.org/wiki/List_of_The_Big_Bang_Theory_episodes";
        // Asynchronously get the document in a new context using the configuration
        var document = await BrowsingContext.New(config).OpenAsync(address);
        // This CSS selector gets the desired content
        var cellSelector = "tr.vevent td:nth-child(3)";
        // Perform the query to get all cells with the content
        var cells = document.QuerySelectorAll(cellSelector);
        // We are only interested in the text - select it with LINQ
        var titles = cells.Select(m => m.TextContent).ToArray();

        var allnum = titles.Count();

        System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\bd.txt");
        for (int i = 0; i <= allnum; i++)
        {
          sw.WriteLine(titles[i] + i);
          Console.WriteLine(titles[i] + i);
        }      
      }
   }
}
使用AngleSharp;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间AngleSharp
{
班级计划
{
静态void Main(字符串[]参数)
{
控制台写入线(“正常启动”);
Mainpar();
Console.ReadKey();
}
静态异步任务Mainpar()
{
//设置配置以支持文档加载
var config=Configuration.Default.WithDefaultLoader();
//从维基百科下载所有《大爆炸理论》剧集的名字
变量地址=”https://en.wikipedia.org/wiki/List_of_The_Big_Bang_Theory_episodes";
//使用配置在新上下文中异步获取文档
var document=await BrowsingContext.New(config).OpenAsync(address);
//此CSS选择器获取所需的内容
var cellSelector=“tr.vevent td:n个孩子(3)”;
//执行查询以获取包含内容的所有单元格
var cells=document.QuerySelectorAll(cellSelector);
//我们只对文本感兴趣-使用LINQ选择它
var titles=cells.Select(m=>m.TextContent.ToArray();
var allnum=titles.Count();
System.IO.StreamWriter sw=新的System.IO.StreamWriter(@“C:\bd.txt”);

对于(int i=0;i您应该正确地关闭流。可能发生的情况是,当您的程序关闭而不刷新文件时,流被强制关闭。请尝试使用using语句进行包装:

    using (var sw = new System.IO.StreamWriter(@"C:\bd.txt"))
    {
        for (int i = 0; i <= allnum; i++)
        {
            sw.WriteLine(titles[i] + i);
            Console.WriteLine(titles[i] + i);
        }
    }
然后可以在主方法中调用它,如下所示:

    static void Main(string[] args)
    {
        Task.Run(async () =>
        {
            Console.WriteLine("ok start");
            await Mainpar();
            Console.ReadKey();
        }).GetAwaiter().GetResult();
    }
    static void Main(string[] args)
    {
        Task.Run(async () =>
        {
            Console.WriteLine("ok start");
            await Mainpar();
            Console.ReadKey();
        }).GetAwaiter().GetResult();
    }