我正在将HTML表格行解析为文本文件。我只需要前两列,在我的情况下td[1]
和td[2]
。我希望输出格式如下,以便我以后可以将它们插入到MySQL数据库中。
Mon, Monday
Tue, Tuesday
Wed, Wednesday
但我目前的文本文件输出如下,我仍然不知道如何在互联网上搜索解决方案后修复它。我想保留下面的格式,但我根本不知道如何在MySQL数据库的每个数据库行中将“Mon”插入column1和“Monday”到column2。
Mon,
Monday
Tue,
Tuesday
Wed,
Wednesday
这是我的代码:
private void btn_parse_Click(object sender, EventArgs e)
{
string FileName = @"..\..\bin\Debug\htm\allaim.htm";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(FileName);
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
HtmlNodeCollection rows = tables[3].SelectNodes(".//tr");
for (int i = 0; i < rows.Count; ++i)
{
HtmlNodeCollection cols = rows[i].SelectNodes(".//td[1]");
for (int j = 0; j < cols.Count; ++j)
{
string value = cols[j].InnerText;
FileStream fs = new FileStream(@"..\..\bin\Debug\txt\" + "allaim.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(value + ",");
sw.Flush();
sw.Close();
fs.Close();
}
HtmlNodeCollection cols2 = rows[i].SelectNodes(".//td[2]");
for (int j = 0; j < cols2.Count; ++j)
{
string value = cols2[j].InnerText;
FileStream fs = new FileStream(@"..\..\bin\Debug\txt\" + "allaim.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(value);
sw.Flush();
sw.Close();
fs.Close();
}
}
MessageBox.Show("Done writing!");
}
也许我这样做的方式不对,我是C#winforms的新手。任何帮助将不胜感激,谢谢你提前! :)
答案 0 :(得分:1)
在StreamWriter.Write()
中使用for loop
而不是StreamWriter.WriteLine()
。
试试这个:
private void btn_parse_Click(object sender, EventArgs e)
{
string FileName = @"..\..\bin\Debug\htm\allaim.htm";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(FileName);
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
HtmlNodeCollection rows = tables[3].SelectNodes(".//tr");
for (int i = 0; i < rows.Count; ++i)
{
HtmlNodeCollection cols = rows[i].SelectNodes(".//td[1]");
for (int j = 0; j < cols.Count; ++j)
{
string value = cols[j].InnerText;
FileStream fs = new FileStream(@"..\..\bin\Debug\txt\" + "allaim.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.Write(value + ",");//use Write() instead of WriteLine()
sw.Flush();
sw.Close();
fs.Close();
}
HtmlNodeCollection cols2 = rows[i].SelectNodes(".//td[2]");
for (int j = 0; j < cols2.Count; ++j)
{
string value = cols2[j].InnerText;
FileStream fs = new FileStream(@"..\..\bin\Debug\txt\" + "allaim.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(value);
sw.Flush();
sw.Close();
fs.Close();
}
}
MessageBox.Show("Done writing!");
}
答案 1 :(得分:1)
最好准备要写的整个字符串。并且还只打开一次文件。并对using
个对象使用IDisposable
关键字。试试这个:
private void btn_parse_Click(object sender, EventArgs e)
{
string FileName = @"..\..\bin\Debug\htm\allaim.htm";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(FileName);
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
using(FileStream fs = new FileStream(@"..\..\bin\Debug\txt\" + "allaim.txt", FileMode.Append))
using(StreamWriter sw = new StreamWriter(fs))
{
HtmlNodeCollection rows = tables[3].SelectNodes(".//tr");
for (int i = 0; i < rows.Count; ++i)
{
HtmlNodeCollection cols = rows[i].SelectNodes(".//td[1]");
HtmlNodeCollection cols2 = rows[i].SelectNodes(".//td[2]");
for (int j = 0; j < cols.Count; ++j)
sw.WriteLine("'" + cols[j].InnerText + "','" + cols2[j].InnerText + "'");
}
sw.Flush();
sw.Close();
fs.Close();
MessageBox.Show("Done writing!");
}
}
如果不是您正在寻找的答案,那么我道歉。