0

我正在尝试使用 selenium Web 驱动程序和 C# 从 Web 搜索中保存数据,一切正常,但是当保存文本时,某些字符似乎编码错误,这是我的代码片段:

        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@fileName, true))
        for (int i = 1; i <= 10; i++)
        {
            String xpath = "/html/body/div[5]/div[2]/div/div[6]/div/div[3]/div/div[2]/div/ol/li[" + i + "]/div"; // google

            String element = driver.FindElement(By.XPath(xpath)).Text;               
            element.Replace(",", " ");
            element = '"' + " " + element + " " + '"'+",";


            {

文件中发现的一些奇怪字符如下所示:

› ...›</p>

任何帮助将非常感激 :)

#############分辨率低于

使用以下方法找到了解决此问题的方法:

        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@fileName, true, UnicodeEncoding.UTF8))
4

1 回答 1

0

默认情况下StreamWriter将以 UTF8 格式写入。这对于 XML 文件应该没问题。你是如何查看文件的?

如果您需要将编码更改为其他内容,则必须使用允许您指定编码的 StreamWriter 构造函数

就像我说的那样,默认的应该可以工作,所以您正在使用的查看器可能期待别的东西。

于 2013-05-18T11:31:05.773 回答