Click here to Skip to main content
15,881,856 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,
I have a scenario where i need to replace
tag with
tag

Initial Code
XML
<div> some text </div>
<div style="any style"> more text</div>



-----------------Code implemented---------------------------------------------------
XML
<div> replaced with <br>
</div> replaced with String.Empty

<div style="any style"> not getting replaced :(


------------------------------------------------------------------------------------

------------------------Code--------------------------------------------------------
C#
public string ReplaceDivTagToBrakeTag(string data)
        {
    return data.Replace("<div>","<br>").Replace("</div>", String.Empty)).ToString();
        }

------------------------------------------------------------------------------------


Final Output
XML
<br> some text
<br> more text




The problem:
It's easy to replace the simple
tag but when a div tag with style came it will not replace it i.e.
not getting replaced.

Please provide a solution :)

Posted
Comments
n.podbielski 16-Oct-12 1:16am    
Cannot you use just 'textContent' property of a tag?

document.getElementsByTagName('div')[0].textContent
sachinDabas 16-Oct-12 3:23am    
I have tried this it not working .
I know one thing only a RegeX can work need help :(
n.podbielski 16-Oct-12 3:31am    
What about $().text()?
Is this not working either?
sachinDabas 16-Oct-12 3:43am    
Can you please elaborate your answer
sachinDabas 16-Oct-12 3:44am    
and I can only user c# code can't use a javascript Its a support project code and can't change the flow :(

C#
using System.Text.RegularExpressions;


[...]

C#
private string ReplaceDivTagToBrakeTag(string data)
{
  Regex regEx = new Regex(@"<div.*>(.*)</div>");

  Match m = regEx.Match(data);

  return "<br>" + m.Groups[1].Value.Trim();
}
 
Share this answer
 
Comments
sachinDabas 19-Oct-12 7:50am    
Hi Michel,
Thanks for this solution i think we are bit closer to the solution but a single problem this is the implementation i used please see the normal <div> is not getting replaced


static string ReplaceDivTagToBrakeTag(string data)
{
Regex regEx = new Regex(@"<div.*>(.*)</div>");

Match m = regEx.Match(data);
return m.Groups[1].Value.Trim();
}
static void Main()
{
string text = "<div> some text </div> <div style=\"any stlye\"> more text </div>";
System.Console.WriteLine("text=[" + text + "]");

System.Console.WriteLine("result=[" + ReplaceDivTagToBrakeTag(text) + "]");
System.Console.ReadLine();
}


And i want to replave there div with BR
You need to handle it (style with div) separately........

data.Replace("<div>", "<br>").Replace("</div>", String.Empty).Replace("<div style=any style>", "<br>").ToString();
 
Share this answer
 
v4
Comments
sachinDabas 16-Oct-12 3:22am    
Hey thanks for the reply but the problem is .Replace("<div style=any style>", "<br>"))
in this We are not sure about style="any style" as it can be any style as Border:1px or Colou:red
so this will not work
Anandkumar D 16-Oct-12 4:57am    
Then you have to go with Regex.Match, i dont know much abt this one, but definitely will get solution with Regex.Match
So maybe you will parse this data as XML.

http://msdn.microsoft.com/en-us/library/cc189056%28v=vs.95%29.aspx

This way you will not have to change it everytime data will change.
I am not sure if xml parser will be sufficient for this. I should if your data will be simple enough and formatted correctly. For more complicated cases use one of non standard HTML parser libraries (http://htmlagilitypack.codeplex.com/[^] this looks promising).

As for parsing it with regex read:

http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html

I know that RegExp is simple, but you have problems already with this, and with RegExp and Html are always problems (been there, done that and it will not happen again).
 
Share this answer
 

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