[英]Append text to the right instead of new line
我正在將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的新手。 任何幫助將不勝感激,謝謝你提前! :)
首先使用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!");
}
最好准備一個完整的字符串來編寫。 並且還只打開一次文件。 ,還可以使用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!");
}
}
如果這不是您正在尋找的答案,那么我道歉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.