Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C#中的HtmlTable中提取数据并排列成一行?_C#_Html Parsing_Html Agility Pack_Webscarab - Fatal编程技术网

如何从C#中的HtmlTable中提取数据并排列成一行?

如何从C#中的HtmlTable中提取数据并排列成一行?,c#,html-parsing,html-agility-pack,webscarab,C#,Html Parsing,Html Agility Pack,Webscarab,我想逐行从HTMLTable中提取数据。但是我在分隔行中的列时遇到了问题。下面我使用的代码给出了一行中的每个单元格。但我希望每一行排成一行,然后是另一行。我该怎么做 HtmlNode table=doc.DocumentNode.SelectSingleNode(“//table[“+tableCounter+”]); foreach(table.SelectNodes(“.//tr/td”)中的var单元格) { 字符串someVariable=cell.InnerText; ReportFi

我想逐行从HTMLTable中提取数据。但是我在分隔行中的列时遇到了问题。下面我使用的代码给出了一行中的每个单元格。但我希望每一行排成一行,然后是另一行。我该怎么做

HtmlNode table=doc.DocumentNode.SelectSingleNode(“//table[“+tableCounter+”]);
foreach(table.SelectNodes(“.//tr/td”)中的var单元格)
{
字符串someVariable=cell.InnerText;
ReportFileWriter(someVariable);
}
tableCounter++;
这是我从这段代码中得到的输出:

原始表格如下所示:

我想要的输出是在列之间有空格:


由于我不知道您的具体网站,我使用以下代码来解析

html表格

您需要安装Nuget->HtmlAgilityPack。 代码:

WebClient-WebClient=new-WebClient();
string page=webClient.DownloadString(“http://www.mufap.com.pk/payout-report.php?tab=01");
HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(第页);
List table=doc.DocumentNode.SelectSingleNode(//table[@class='mydata'])
.后代(“tr”)
.Skip(1)
其中(tr=>tr.Elements(“td”).Count()>1)
.Select(tr=>tr.Elements(“td”).Select(td=>td.InnerText.Trim()).ToList())
.ToList();
字符串结果=string.Empty;
foreach(表[0]中的var项)
{
结果=结果+“”+项目;
}
控制台写入线(结果);
网站第一行:

您将得到以下结果:

abl现金基金货币市场2010年7月31日如何获取此类数据?在一行中,我已经更新了我的代码,您可以检查它是否对您有帮助。
            WebClient webClient = new WebClient();
            string page = webClient.DownloadString("http://www.mufap.com.pk/payout-report.php?tab=01");

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(page);

            List<List<string>> table = doc.DocumentNode.SelectSingleNode("//table[@class='mydata']")
                        .Descendants("tr")
                        .Skip(1)
                        .Where(tr => tr.Elements("td").Count() > 1)
                        .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
                        .ToList();
             string result = string.Empty;
        foreach (var item in table[0])
        {
            result = result + "        " + item;
        }
        Console.WriteLine(result);