【问题标题】:Append text to the right instead of new line将文本附加到右侧而不是新行
【发布时间】:2013-12-02 06:46:22
【问题描述】:

我正在将 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 的新手。任何帮助将不胜感激,在此先感谢您! :)

【问题讨论】:

    标签: c# mysql winforms parsing html-agility-pack


    【解决方案1】:

    最好准备一个完整的字符串来编写。并且只打开一次文件。并且还对 IDisposable 对象使用 using 关键字。试试这个:

    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!");
        }
    }
    

    如果这不是您想要的答案,那么我很抱歉。

    【讨论】:

    • 嗨,Dmitry,我想这样做(只打开一次文件并将两个 for 循环结合起来),但我不知道该怎么做。非常感谢你! :)
    • foreachCannot convert type 'HtmlAgilityPack.HtmlNode' to 'HtmlAgilityPack.HtmlNodeCollection' 时出现错误。任何的想法? :)
    • 是的,只需返回 for 循环。我已经编辑了代码。或者将row的类型改为HtmlNode
    • 嗨,Dmitry,代码似乎工作正常,结果生成速度更快,谢谢! :) 我知道我不应该问,但我一直在尝试很长时间将“星期一,星期一”插入 MySQL 数据库列“短”和“长”。如果你能抽出时间指导我第二部分,那就太好了。谢谢!
    • string query = "INSERT INTO 'table'('Short', 'Long') VALUES (" + String.Join("),(", File.ReadAllLines(filename)) + ")";
    【解决方案2】:

    在第一个 for loop 中使用 StreamWriter.Write() 而不是 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!");
        }
    

    【讨论】:

    • 非常感谢!!一个小错误,但对我来说是一个大知识! :D
    猜你喜欢
    • 1970-01-01
    • 2015-05-25
    • 2016-09-15
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多