All well and good but I can be a bit of a perfectionist sometimes and I like my HTML tags to be in lowercase so that it becomes XHTML compliant. Typically, the feed of data contains tags that are all in uppercase. Even by solving this issue, I'm not really onto a winner with this feed as the tags aren't closed off in most cases but I figure making the tags lower case is a start!
Anyway, I did start off by doing a list of replaces after the string:
strOriginalString.Replace("<UL>", "<ul>").Replace("</UL>", "</ul>").Replace("<LI>", "<li>") etc etc
I realised this could get very long and they may also start using tags that I hadn't covered so I decided a regular expression was probably the way to go. Not being a genius (or anything remotely close) with these, it took a bit of Googling and I eventually came up with a solution.I'm not sure it is the best way but it does seem to work.....First I created a function on the ASP.Net code behind as this:
Private Function ToLowerCase(ByVal m As Match) As String
Dim objSB As New StringBuilder
For intLoop = 0 To m.Captures(0).Value.Count - 1
objSB.Append(m.Captures(0).Value(intLoop).ToString.ToLower)
Next
Return objSB.ToString
End Function
Dim objSB As New StringBuilder
For intLoop = 0 To m.Captures(0).Value.Count - 1
objSB.Append(m.Captures(0).Value(intLoop).ToString.ToLower)
Next
Return objSB.ToString
End Function
This should receive a full tag and go through each character (including < and >) and set them to lowercase before returning the full tag now in lowercase.
Finally I had to set up the RegEx on the string in question:
litText.Text = Regex.Replace(strOriginalString, "<(.|\n)+?>", New MatchEvaluator(AddressOf ToLowerCase))
This seemed to solve the issue for me. Maybe in the future I will find a quicker/cleaner way....