views:

61

answers:

3

Can anyone tell me how to modify HTML at runtime please?

A user has pasted a table from Word into a text editor control (FCKEditor)

The editor control automatically converts to HTML however I want to change the HTML before saving to database.

So, I want to make this

<table width="600" height=100>

into this

<table>

before saving string to the database.

Have tried using XmlDocument but LoadXml method doesn't like

&nbsp;

Any ideas?

A: 

HTML Agility

Mark Brackett
+1  A: 

Use the HTML Agility Pack (free / open source) to load the HTML into a DOM. Then you can manipulate it there.

Keltex
+2  A: 

Alright, I'm going to get bashed by the agility pack people here, but if you've got a known pattern that you're looking to target its okay to use RegEx.

'Source Text
Dim text = "<table width=""600"" " & vbNewLine & "height=100>" & vbNewLine & "<td>hello</td>" & vbNewLine & "</table>"
'Replace all table tags just empty table tags
text = System.Text.RegularExpressions.Regex.Replace(text, "<table.*?>", "<table>", System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Singleline)
Chris Haas
I agree - regular expressions are the way to go here. Loading up a DOM in memory just to do a search and replace seems like overkill to me.
camainc