0

是否可以使用 streamreader 查找与标签匹配的所有行,然后编辑它们之间的内容?

到目前为止,我可以将 html 文件的所有内容读入富文本框中,但我希望它找到具有 h4 标签的每一行并将其加载到 textbox1、2、3 等中。

我该怎么做?你能举个例子吗,我是新手。

    private void loadToolStripMenuItem_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog dlgOpen = new OpenFileDialog())
        {
            try
            {
                // Available file extensions
                dlgOpen.Filter = "All files(*.*)|*.*";
                // Initial directory
                dlgOpen.InitialDirectory = "L";
                // OpenFileDialog title
                dlgOpen.Title = "Open";
                // Show OpenFileDialog box
                if (dlgOpen.ShowDialog() == DialogResult.OK)
                {
                    // new StreamReader
                    StreamReader sr = new StreamReader(dlgOpen.FileName, Encoding.Default);
                    // Get all text from the file
                    string str = sr.ReadToEnd();

                    // CloseStreamReader
                    sr.Close();
                    // Show text
                    richTextBox1.Text = str;

                }

            }
4

1 回答 1

0

From You should use HtmlAgilityPack. and could you give an example of how to use it with html agility pack then?

Follow How to use HTML Agility pack.

You can find examples as well.Here

Please do not try to use Regular Expressions to find and replcase. Because this great answer RegEx match open tags except XHTML self-contained tags begs to avoid that. I was told about it in comments

However I had used them 100% successfully with 110% limited scope. Limited mean my e.g. I have no other tags between any <td> sometext </td> and I wanted to replace all of them with <td> sometext and some newtext </td> and I had done it successfully. But I was 100% sure what number of tags and what maximum depth I had in my HTML with no Unexpected and Irregular behavior. If you have such limited and well defined HTML then These Regular Expressions might help.

于 2012-11-16T05:22:58.413 回答