我从网站获取数据并插入DataTable dt
。
if (checked = true)
{
while (true)
{
breakIt = true;
try
{
string html = browser_array[idx].FindElementByXPath("id('tabbox')").GetAttribute("outerHTML");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
HtmlNodeCollection nodesMatchingXPathlop = doc.DocumentNode.SelectNodes("//*[contains(@class,'tabbox_F')]/table/tbody/tr");
if (nodesMatchingXPathlop.Count == 0)
{
break;
}
else
{
for (int j = 0; j < nodesMatchingXPathlop.Count; j++)
{
try
{
listHeader[1].Clear();
HtmlNodeCollection listTD3 = nodesMatchingXPathlop[j].SelectNodes("td");
if (nodesMatchingXPathlop[j].SelectNodes("td").ToString() == "")
return;
if (nodesMatchingXPathlop[j].SelectNodes("td").Count == 10)
{
if (listHeader[1].Count == 0)
continue;
else
Insert(dt, listHeader[1]);
}
else
{
continue;
}
}
catch (Exception ex) { throw; }
}
}
if (breakIt)
{
break;
}
}
catch (Exception ex) { throw; }
}
}
所以,这个函数是Insert(DataTable dt, List<String> listHeader)
:
static void Insert(DataTable dt, List<String> listHeader)
{
DataRow row = dt.NewRow();
string[] array = listHeader.ToArray();
string newArray = "";
newArray = Regex.Replace(array[1], @"\s+", " ");
string arrayHindiCap2 = "";
arrayHindiCap2 = Regex.Replace(array[2], @"\s+", " ").Replace(" ", "");
string arrayHindiCap5 = "";
arrayHindiCap5 = Regex.Replace(array[5], @"\s+", " ").Replace(" ", "");
try
{
if (!string.IsNullOrWhiteSpace(arrayHindiCap2))
{
InsertCondition(arrayHindiCap2, arrayHindiCap5, newArray, listHeader, array);
}
else if (!string.IsNullOrWhiteSpace(arrayHindiCap5))
{
InsertCondition(arrayHindiCap2, arrayHindiCap5, newArray, listHeader, array);
}
}
catch (Exception ex) { throw; }
}
我有一张这样的表:
Option B0_1 Col 1 Col 2
op1 10 89
当条件更正时,我将得到DataTable中的行索引。
static void InsertCondition(string arrayHindiCap2, string arrayHindiCap5, string newArray, List<String> listHeader, string[] array)
{
try
{
index = dt.AsEnumerable()
.Select((rowF, idx) => new { rowF, idx })
.Where(item =>
GetFirstB(numB) == (item.rowF.Field<string>("B0_1") != null ? item.rowF.Field<string>("B0_1") : "") &&
GetFirstWord(optionString) == (item.rowF.Field<string>("Option") != null ? item.rowF.Field<string>("Option") : ""))
.Select(item => item.idx)
.ToArray();
}
catch(Exception ex) { throw; }
}
我定义GetFirstB()
和GetFirstWord()
:
private static string GetFirstWord(string str)
{
try
{
return string.Concat(str.Split(' ').Take(2));
}
catch (Exception ex) { throw; }
}
private static string GetFirstB(string str)
{
try
{
if (str == null)
return null;
if (str.Equals(" "))
return null;
string[] strNew;
strNew = str.Split(' ');
strNew = strNew.Where(c => c != "").ToArray();
return string.Concat(strNew.FirstOrDefault());
}
catch (Exception ex) { throw; }
}
关注@ScottChamberlain和@PhilipKendall建议。 我编辑我的代码并再次尝试调试。
复制后异常将详细信息复制到剪贴板,它显示如下:
System.InvalidOperationException was caught
HResult=-2146233079
Message=Collection was modified; enumeration operation might not execute.
Source=System.Data
StackTrace:
at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Enumerable.<SelectIterator>d__7`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at LAPSOFT.Program.InsertCondition(String arrayHindiCap2, String arrayHindiCap5, String newArray, List`1 listHeader, String[] array) in d:\lapsoft-project\CA CUOC - Copy\Program.cs:line 418
at LAPSOFT.Program.Insert(DataTable dt, List`1 listHeader) in d:\project\CheckCopy\Program.cs:line 548
InnerException: