简体   繁体   中英

Remove all HTML tags and do a carriage return on <BR> in C#

I am creating a HTML to text parser. I need to remove all HTML elements and want to do a carriage return everytime there is a <BR> and then remove the <BR> as well after so there are no HTML tags left. I then want to parse the text for a certain string that is in the combobox. Thank you in advance for your help.

private void navigateWeb_Click(object sender, EventArgs e)
    {

        openFD.Title = "Select your configuration file";
        openFD.InitialDirectory = "C:";
        openFD.FileName = "";
        openFD.Filter = "Config File (*.cfg)|*.cfg|Text File (*.txt)|*.txt|All Files (*.*)|*.*";
        openFD.ShowDialog();
        MyURL = openFD.FileName;
        //Open and read file
        System.IO.StreamReader objReader;
        objReader = new System.IO.StreamReader(MyURL);
        richTextBox1.Text = objReader.ReadToEnd();

        var lines = File.ReadAllLines(MyURL)
            .Select(l => l.Trim())
            .Where(l => l.StartsWith(comboBox1.Text));
        textBox1.Text = String.Join(Environment.NewLine, lines);


    }

** * ** * *** UPDATE * ** * * Here is the solution that got the job done:

 public static string RemoveHTML(string text)
    {
        text = text.Replace("&nbsp;", " ").Replace("<br>", "\n");
        var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
        return oRegEx.Replace(text, string.Empty);

    } 
private void navigateWeb_Click(object sender, EventArgs e)
{

    openFD.Title = "Enter URL in the box below";
    openFD.InitialDirectory = "C:";
    openFD.FileName = "http://msnconf/configtc.aspx?IP=10.6.64.200&m=c";
    openFD.Filter = "HTTP://|*.*|Config File (*.cfg)|*.cfg|Text File (*.txt)|*.txt|All Files (*.*)|*.*";



    //openFD.ShowDialog();
    if (openFD.ShowDialog() == DialogResult.Cancel)
    {
        //MessageBox.Show("cancel button clicked");
    }
    else
    {
        MyURL = openFD.FileName;

        webBrowser1.Visible = true;
        richTextBox1.Visible = false;
        permitACL.Enabled = true;


        //webBrowser1.Navigate(new Uri(MyURL.SelectedItem.ToString()));
        webBrowser1.Navigate(MyURL);
        //Open and read file
        System.IO.StreamReader objReader;
        objReader = new System.IO.StreamReader(MyURL);
        richTextBox1.Text = objReader.ReadToEnd();




        //Read all lines of file
        //            String lines = objReader.ReadToEnd();
        String[] crString = { "<BR>&nbsp;" };
        String[] aLines = richTextBox1.Text.Split(crString, StringSplitOptions.RemoveEmptyEntries);
        //            String[] lines = File.ReadAllLines(MyURL);
        String noHtml = String.Empty;

        for (int x = 0; x < aLines.Length; x++)
        {
            if(permitACL.Checked)
            {

                if (aLines[x].Contains("permit"))
            {
                noHtml += (RemoveHTML(aLines[x]) + "\r\n");
            }

            }

            if (aLines[x].Contains(comboBox1.Text))
            {
                noHtml += (RemoveHTML(aLines[x]) + "\r\n");
            }
        }

        //Find lines that match our text in the combobox
        //lines.Select(l => l.Trim());
        //.Where(l => l.StartsWith(comboBox1.Text));



        //Print results to textbox
        textBox1.Text = String.Join(Environment.NewLine, noHtml);
    }

}

我建议您使用HTML Agility Pack-这是一个HTML解析器,您可以使用XPath语法进行查询。

public static string RemoveHTML(string text)
{
        text = text.Replace("&nbsp;", " ").Replace("<br>", "\n");
        var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
        return oRegEx.Replace(text, string.Empty);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM