• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

ASP and Invalid XML Characters

I'm trying to create an XML document (for google base) using asp. However i keep getting invalid character errors. My data contains regular text so there are ' & - / in the text. So i want to create a function to prepare the text drawn from the DB to be XML safe. Heres roughly what i want to do:

<xmlattribute><%=prepareXML(ObjRS("column"))%></xmlattribute>

<%
Function prepareXML(text)
      prepareXML = Replace(text,"&","???")
End Function
%>

My question is what are all the possible characters i need to replace, and what are the values i need to replace them with. Thanks
0
alex_wareing
Asked:
alex_wareing
  • 2
1 Solution
 
hongjunCommented:
Try this

Function prepareXML(text)
     text = Replace(text,"&", "&amp;")
     text = Replace(text,"'", "&apos;")
     text = Replace(text,"""", "&quot;")
     text = Replace(text, ">", "&gt;")
     text = Replace(text,"<","&lt;")

     prepareXML = text
End Function



hongjun
0
 
Ryan ChongCommented:
or using server.HTMLEncode method, like:

Function prepareXML(text)
     prepareXML = server.HTMLEncode(text)
End Function

just my $0.02, cheers
0
 
hongjunCommented:
Note the difference.

<%
Dim str

str = "Hello"" World"
Response.Write "<b>prepareXML1</b> is the better one<br><br>"
Response.Write prepareXML1(str)
Response.Write "<br>"
Response.Write prepareXML2(str)


Function prepareXML1(text)
     text = Replace(text,"&", "&amp;")
     text = Replace(text,"'", "&apos;")
     text = Replace(text,"""", "&quot;")
     text = Replace(text, ">", "&gt;")
     text = Replace(text,"<","&lt;")

     prepareXML1 = text
End Function

Function prepareXML2(text)
     prepareXML2 = Server.HTMLEncode(text)
End Function
%>


hongjun
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now