简体   繁体   English

C# 以特殊方式将列表的元素添加到二维数组

[英]C# Adding elements of a List to a 2 dimensional Array in a special way

my problem is the following (C# enviroment): I got a table in a HTML document.我的问题如下(C# 环境):我在 HTML 文档中有一个表格。 I extracted the tables using HTMLAgilityPack and Regular Expressions.我使用 HTMLAgilityPack 和正则表达式提取了表格。 All the cells are saved in a List of strings.所有单元格都保存在字符串列表中。 Now I want so save these tables in a two dimensional array like a common table.现在我想将这些表保存在一个二维数组中,就像一个普通表一样。 The problem is, that the rows are not having the same length.问题是,行的长度不同。 The tables are written in this way (the ... dots are empty space):表格是这样写的(...点是空格):

| | aaa |啊! 123 | 123 | 456 |第456章

| | ....... | ....... | 986 | 986 | 468 |第468章

| | bbb | bbb | 507 | 507 | 206 | 206 |

| | ........ | ........ | 450 | 450 | 256 | 256 |

The "aaa" in the first column applies to the first and second row, the "bbb" to the 3. and 4. row, and so on.第一列中的“aaa”适用于第一行和第二行,“bbb”适用于第 3 行和第 4 行,依此类推。 Now I need some kind of logic to tell the Array to fill it from the List in the right way like:现在我需要某种逻辑来告诉数组以正确的方式从列表中填充它,例如:

| | aaa |啊! 123 | 123 | 456 |第456章

| | aaa |啊! 986 | 986 | 468 |第468章

| | bbb | bbb | 507 | 507 | 206 | 206 |

| | bbb | bbb | 450 | 450 | 256 | 256 |

None of these ideas are fix, we don't have to use a List or an Array, these are just my first ideas.这些想法都不是固定的,我们不必使用列表或数组,这些只是我的第一个想法。 If you have an idea for a logic to fill the Array or a complete different, but better way, I would be very thankfull!如果您对填充数组的逻辑或完全不同但更好的方法有想法,我将非常感激!

IMPORTANT EDIT: the missing cell could be in any column of the table, not just in the first position!!!重要编辑:丢失的单元格可能在表格的任何列中,而不仅仅是在第一个位置!!! Like:喜欢:

| | aaa |啊! 123 | 123 | 456 |第456章

| | aaa |啊! ....... | ....... | 468 |第468章

| | bbb | bbb | 507 | 507 | 206 | 206 |

| | bbb | bbb | 450 | 450 | ....... | ....... |

Now the dots in the second row are belonging to the "123" and the dots in the last row are belonging to the " 206".现在第二行的点属于“123”,最后一行的点属于“206”。

        string[,] DataArray = new string[4, 3]; // just an example size for the table above
     List<string> Data = new List<string>(); // the cells from the HTML table are saved here

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(myHtmlDoc);

        foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
        {
            foreach (HtmlNode row in table.SelectNodes("tr"))
            {
                foreach (HtmlNode cell in row.SelectNodes("th|td"))
                {
                    Data.Add(cell.InnerText);
                }
            }
        }
        
        for (int i = 0; i < DataArray.GetLength(0); i++)
        {
            for (int j = 0; j < DataArray.GetLength(1); j++)
            {                                  
                // some logic in here to pick the right part of the List and put it in the 
                // right position in the Array
            }
        }

Here is a solution that will work for you using Linq and returning a Dictionary.这是一个适用于您使用 Linq 并返回字典的解决方案。 My apologies, as I appreciate it may not make a whole lot of sense if you are new to C#.我很抱歉,我很感激,如果您是 C# 新手,这可能没有多大意义。 I can maybe come back later and add some more explanation.我也许可以稍后回来并添加更多解释。

I've made the assumption that the values can be parsed to int.我假设这些值可以解析为 int。 The values that cannot be parsed to int is simply discarded in this solution.无法解析为 int 的值在此解决方案中被简单地丢弃。

string runningKey = "";

Dictionary<string, List<int>> dictionary =
    doc.DocumentNode.SelectNodes("//table")
        .SelectMany(t => t.SelectNodes("tr"))
        .Select(tr => tr.SelectNodes("td|th").ToList())
        .Where(tds => tds.Any())
        .Select(
            (tds) =>
            {
                runningKey = string.IsNullOrWhiteSpace(tds.First().InnerText) ? runningKey : tds.First().InnerText;

                return
                    new
                    {
                        Key = runningKey,
                        Values =
                            tds
                                .Skip(1)
                                .Select(td => int.TryParse(td.InnerText, out int result) ? result : -1)
                                .Where(n => n != -1)
                                .ToList()
                    };
            })
        .GroupBy(v => v.Key)
        .ToDictionary(g => g.Key, g => g.SelectMany(v => v.Values).ToList());

Hope this helps!希望这可以帮助!

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

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