Monday, April 8, 2013

ASP.NET - Add Class Tags to HTML with XML Node Processing

Here is how you can process html and pick out html tags and add class attributes to them:

Imports System.Xml


    Private Function HandleClassTags(ByVal astrContent As String) As String

        Dim objxmlDoc As New XmlDocument
        Dim objNodeList_Html As XmlNodeList
        Dim strXML As String = ""
        Dim dt_ClassTags As DataTable
        Dim strResult As String = ""

        Try
            dt_ClassTags = GetClassTags() 'return datatable with one column "parent_tag" and one column as "class" ... the value for class will be assigned to the html tag in "parent_tag"

            If dt_ClassTags.Rows.Count > 0 Then

                'replace all & symbols with '---amp---'
                strXML = "<root>" & astrContent.Replace("&", "---amp---") & "</root>"
                objxmlDoc.LoadXml(strXML)

'first go through and remove the existing class set for the html tag if one exists
                For Each row As DataRow In dt_ClassTags.Rows
                    objNodeList_Html = objxmlDoc.GetElementsByTagName(row("parent_tag").ToString)
                    For Each xmlElem As System.Xml.XmlElement In objNodeList_Html
                        If xmlElem.GetAttribute("class") IsNot Nothing OrElse xmlElem.GetAttribute("class") <> "" Then
                            xmlElem.RemoveAttribute("class")
                        End If
                    Next
                Next

'now add the tags from the table as described above
                For Each row As DataRow In dt_ClassTags.Rows
                    objNodeList_Html = objxmlDoc.GetElementsByTagName(row("parent_tag").ToString)
                    For Each xmlElem As System.Xml.XmlElement In objNodeList_Html
                        If xmlElem.GetAttribute("class") Is Nothing OrElse xmlElem.GetAttribute("class") = "" Then
                            xmlElem.SetAttribute("class", row("class").ToString)
                        End If
                    Next
                Next

                strResult = objxmlDoc.OuterXml
                strResult = strResult.Replace("<root>", "").Replace("</root>", "").Replace("---amp---", "&")
            Else
                strResult = astrContent
            End If

            Return strResult

        Catch ex As Exception
            Throw ex
        Finally
            If dt_ClassTags IsNot Nothing Then
                dt_ClassTags.Dispose()
                dt_ClassTags = Nothing
            End If
            If objxmlDoc IsNot Nothing Then
                objxmlDoc = Nothing
            End If
        End Try
    End Function