B4A Class EscapeXml - Code snippet

Discussion in 'Additional libraries, classes and official updates' started by Erel, Dec 15, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The following code escapes the five XML entities. If you are using a library such as XmlBuilder then it is not required to escape the text as the XML writer will do it automatically.

    However if you use a template for the XML file and need to fill some of the fields then this code can be helpful.

    Code:
    Public Sub EscapeXml(Raw As StringAs String
       
    Dim sb As StringBuilder
       sb.Initialize
       
    For i = 0 To Raw.Length - 1
         
    Dim c As Char = Raw.CharAt(i)
         
    Select c
           
    Case QUOTE
             sb.Append(
    """)
           
    Case "'"
             sb.Append(
    "'")
           
    Case "<"
             sb.Append(
    "&lt;")
           
    Case ">"
             sb.Append(
    "&gt;")
           
    Case "&"
             sb.Append(
    "&amp;")
           
    Case Else
             sb.Append(c)
         
    End Select
       
    Next
       
    Return sb.ToString
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice