繁体   English   中英

删除所有HTML标记并在回车符上 <BR> 在C#中

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

我正在创建一个HTML文本解析器。 我需要删除所有HTML元素,并希望每次有<BR>都进行回车,然后再删除<BR> ,这样就没有HTML标记了。 然后,我想为组合框中的某个字符串解析文本。 预先感谢您的帮助。

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);


    }

** * ** * *** 更新 * ** * *这是完成工作的解决方案:

 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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