【问题标题】:Can I programatically copy my html selection我可以以编程方式复制我的 html 选择吗
【发布时间】:2013-08-30 09:05:23
【问题描述】:

我有一个 html 文档,在解析后只包含格式化的文本。我想知道是否可以像鼠标选择它 + 复制 + 粘贴到新的文本文档中那样获取它的文本?

我知道这在 Microsoft.Office.Interop 中是可能的,我有 .ActiveSelection 属性来选择打开的 Word 的内容。

我需要找到一种方法以某种方式加载 html(可能在浏览器对象中),然后复制其所有内容并将其分配给字符串。

var doc = new HtmlAgilityPack.HtmlDocument();
var documetText = File.ReadAllText(myhtmlfile.html, Encoding.GetEncoding(1251));
documetText = this.PerformSomeChangesOverDocument(documetText);
doc.LoadHtml(documetText);
var stringWriter = new StringWriter();
AgilityPackEntities.AgilityPack.ConvertTo(doc.DocumentNode, stringWriter);
stringWriter.Flush();
var titleNode = doc.DocumentNode.SelectNodes("//title");
if (titleNode != null)
{
    var titleToBeRemoved = titleNode[0].InnerText;
    document.DocumentContent = stringWriter.ToString().Replace(titleToBeRemoved, string.Empty);
}
else
{
    document.DocumentContent = stringWriter.ToString();
}

然后我返回文档对象。问题是字符串的格式并不总是像我想要的那样

【问题讨论】:

  • 你有代码要显示吗?你试过什么?
  • 我已经尝试过 html 敏捷包,最初我的 html 有点乱,所以我正在对它们进行一些替换。之后,我尝试将 doc.DocumentNode 分配给 StringWriter。我将更新我的问题。

标签: c# html text-parsing html-rendering


【解决方案1】:

您应该可以只使用StreamReader,并且在阅读每一行时,只需使用StreamWriter 将其写出来

这样的内容会一直读取到您的文件末尾,然后将其保存到一个新文件中。如果您需要在文件中执行额外的逻辑,我会插入一条评论,让您知道在哪里执行所有这些操作。

private void button4_Click(object sender, EventArgs e)
        {
            System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\XXX\\XXX\\XXX\\test2.html");
            String line;
            using (System.IO.StreamReader reader = new System.IO.StreamReader("C:\\XXX\\XXX\\XXX\\test.html"))
            {
                //Do until the end
                while ((line = reader.ReadLine()) != null) {
                //You can insert extra logic here if you need to omit lines or change them
                writer.WriteLine(line);
                }
                //All done, close the reader
                reader.Close();
            }
            //Flush and close the writer
            writer.Flush();
            writer.Close();

        }

您也可以将其保存为字符串,然后随心所欲地使用它。您可以使用新行来保持相同的格式。

编辑以下将考虑您的标签

  private void button4_Click(object sender, EventArgs e)
        {
            String line;
            String filetext = null;
            int count = 0;
            using (System.IO.StreamReader reader = new System.IO.StreamReader("C:\\XXXX\\XXXX\\XXXX\\test.html"))
            {
              while ((line = reader.ReadLine()) != null) { 
                if (count == 0) {
                    //No newline since its start
                    if (line.StartsWith("<")) {
                        //skip this it is formatted stuff
                    }
                    else {
                    filetext = filetext + line; 
                    }
                    }
                else {
                    if (line.StartsWith("<"))
                    {
                        //skip this it is formatted stuff
                    }
                    else
                    {
                        filetext = filetext + "\n" + line;
                    }
                }
                count++;                           
           }                
            Trace.WriteLine(filetext);                  
            reader.Close();
            }          
        }

【讨论】:

  • 我认为 OP 想要修改呈现的 HTML 而不仅仅是复制文本文件。
  • "如果我用鼠标选择它 + 复制 + 粘贴到新的文本文档中,是否可以像我一样获取它的文本?"我的回答会做到这一点。如果他需要执行额外的逻辑,他可以在循环中执行。似乎他有一个已经被解析的文档,他只是想将它复制到一个新文件中?不确定我是否遗漏了什么
  • 从他之前的问题stackoverflow.com/questions/18350208/… 我想,“解析”的意思是“在浏览器中渲染”。
  • @Corak 不知道原始问题。如果 OP 可以发表评论并说这不是他想要的,我将删除。
  • @sealz 我还没有解析文档。您正在使用 .txt 文件进行阅读,但 .html 文件不会发生这种情况。
猜你喜欢
  • 1970-01-01
  • 2011-02-07
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2022-07-04
  • 1970-01-01
相关资源
最近更新 更多