stefan
stefan

Reputation: 439

using streamreader to edit between html tags

Is it possible to use streamreader to find all the lines that match tags and then edit the content inbetween them?

So far i can read all the content of the html file into a rich text box, but i would prefer for it to find each line that has an h4 tag and load it into textbox1, 2 ,3 etc.

how would i do this? Could you please give an example, i am new to this.

    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;

                }

            }

Upvotes: 0

Views: 873

Answers (1)

Sami
Sami

Reputation: 8419

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.

Upvotes: 0

Related Questions