Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to replace the html tags "b" & "p" from my text and with the following code below, I am getting the output of my text with the html tags.
C#
public string GetArticleText()
    {
    string htmlStr = "";
    string strConnectionString = ConfigurationManager.ConnectionStrings["######"].ConnectionString;
    SqlConnection thisConnection = new SqlConnection(strConnectionString);
    SqlCommand thisCommand = thisConnection.CreateCommand();
    thisCommand.CommandText = "############";
    thisConnection.Open();
    SqlDataReader reader = thisCommand.ExecuteReader();
    while (reader.Read())
    {
        string Name = reader.GetString(0);
        string Pass = reader.GetString(1);

        Pass = Pass.Replace("<br>", "");
        Pass = Pass.Replace("<p>", "");
        Pass = Pass.Replace("</p>", "");
        htmlStr += "<tr><h2>" + Name + "</h2></tr><tr>" + Pass + "</tr>";
    }

    thisConnection.Close();
    return htmlStr;
    }

Client-side:
HTML
<table width="100%" align="center" cellpadding="2" cellspacing="2" border="0" >
       <%=GetArticleText()%>
   </table>




Is there something i missing or doing incorrectly in my above code? please advice. Thanks.
Posted
Updated 17-Oct-14 6:08am
v3

String.Replace will work but you can also look at String.Remove. In your code you are receiving data from a reader, and then replacing the html.

Prevention is better than cure, so I would suggest that you remove it from where you are inserting it from. Take the contents from the tinymce editor and send it to a new string variable and use the method below.

C#
String HtmlLine = "</br>";
String NormalLine = HtmlLine.Replace("</br>", "");


Or If there is lots of html elements to remove
HTML
</br>
, then iterate through the words in the string and remove each occurrence of html on the fly. You could use a Select Case or If statement to check for Html entries in the words and remove them using the methods mentioned above accordingly.

While iterating each word that is now not html; add each word to a dictionary, and then reiterate the dictionary values to retrieve your sentence from the first dictionary key to the last dictionary key. You can then construct your new string without html and add it to your database. See a reference of dictionary here.

There are a hundred other options to do this, in many different ways.

The simplest would be to use a If statement to check for html entries and remove them by iterating each word. You can find many results on iterating each word on Google.

C#
String HtmlLine = "</br>";
String NormalLine = HtmlLine.Replace("</br>", "");


Hope this helps. If you have any questions, let me know.

If you found this helpful, please mark it as a solution.
 
Share this answer
 
v3
if <p> and others are displayed in your browser, it must not be present as this in the actual string.

For example, to get the <p> displayed in my solution, I have entered &lt;p&gt; ( &lt; for < and &gt; for >)

If your source string (in your database) contains those strings, your browser will interpret those and will display the interpreted string. To be sure of this, you can look at the source code of your page (by pressing F12 in most browser).

And to correct your problem, you may have to replace :
- &lt;p&gt; rather than <p>
- &lt;/p&gt; rather than </p>
but you have a lot of other special strings. (&amp; is & for example.)
 
Share this answer
 
Comments
miss786 17-Oct-14 12:07pm    
Thank you so much for solution and you explanation, into why I was experiencing this issue. You have been great help. Much appreciated. Many thanks & have a great weekend! =D
[no name] 17-Oct-14 12:58pm    
Miss786, Why don't you create a dictionary of all of the html elements in your page and then iterate through each dictionary value, and if the value is a match, you can then simply remove or replace it from your string.

You could use a Function based on RegEx (Regular Expression) to check if there is a match in the dictionary items, and construct a new variable without the html formatting.
XML
Pass = Pass.Replace("<br>", "");
Pass = Pass.Replace("<p>", "");
Pass = Pass.Replace("</p>", "");

Check what tag is used <br> or <br/>
 
Share this answer
 
Comments
miss786 17-Oct-14 7:27am    
Thank you for your reply. I have corrected the tags for <br/>, but it still appearing on the client-side. Please advice further.
MukeshSagar 17-Oct-14 7:49am    
Show the screen shot of the output, so that i can suggest solution
miss786 17-Oct-14 10:10am    
Thank you for your reply & apology for the late response. I have copied the output as text, in my above post for further reference. I was unable to attach screenshot of my output, as I could not find option, on the forum to do so.

I also would like to inform, I am insert the articles into database using "tinymce'' text editor, could this be the reason, why the 'replace' string is not rendering correctly? Thank you
HTML
Pass = Pass.Replace("<br>", "");
Pass = Pass.Replace("</br>", "");
Pass = Pass.Replace("<p>", "");
Pass = Pass.Replace("</p>", "");
 
Share this answer
 
v2
Comments
miss786 17-Oct-14 10:11am    
Hi, thank you for your help. Sadly, I have already tried this above in my code, which does not remove the html tags on the client-side.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900