Never give up - LEO

人 只有在合适的地方 才能体现出最大的价值
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

ASP中字符丢失特奇怪问题?

Posted on 2007-02-01 18:01  lizhiwen  阅读(425)  评论(5编辑  收藏  举报

本人最近在维护一个asp做的网站,里面有段代码如下:
<% sql="select a.id,a.title,b.projectId from tblIBBemail a" & _ " inner join tblibbautoemailcategory b on a.id = b.emailid" & _ " order by b.projectid,a.Title" set rec=server.createobject("ADODB.Recordset") rec.open sql, conn, 3, 3 if not rec.eof then do until rec.eof stitle = rec("title") stitle = replace(stitle,"&","&") stitle = replace(stitle,"'","'") stitle = replace(stitle,"""",""") response.write "" & chr(10) rec.movenext loop end if %> <xml id="emaillistxml">
 <emails>
<%
sql="select a.id,a.title,b.projectId from a" & " inner join b on a.id = b.emailid" 
 
set rec=server.createobject("ADODB.Recordset")
rec.open sql, conn, 3, 3
 
if not rec.eof then
 do until rec.eof
  stitle = rec("title")
  stitle = replace(stitle,"&","&amp;")
  stitle = replace(stitle,"'","&apos;")
  stitle = replace(stitle,"""","&quot;")
   str = "<email id='" & rec("ID") & "' title='" & stitle & "'"
  response.write  str
 
   response.write " projectid='" & rec("projectId") & "' />" & chr(10)
  rec.movenext
 loop
end if
%>
 </emails>
</xml>
这段代码用意是组成一个xml格式的文件,可是组成完毕后,却显示有误,仔细查找,原来是里面有非法字符,字符如下:“2222 — 2222222”   ;“ff – ff ”,请注意这里面的两个横杠,前面一个是中文全角打出来的,十六进制是“A1 AA”,后面这个不知道谁录进数据库的,十六进制是“20 96 20”;凡是出现了这两个字符的字段在str = "<email id='" & rec("ID") & "' title='" & stitle & "'"这句语句中连接时就会出现问题,最后的单引号不见了。打个比方 rec("ID") 不考虑
如果stitle="2222 — 2222222",那么str的结果是"<email id='1' title='2222 — 2222222 ",最后应该还有一个单引号确不见了。我再试着把str = "<email id='" & rec("ID") & "' title='" & stitle & "'"改成str = "<email id='" & rec("ID") & "' title='" & stitle & "'testtest",再次运行,结果还是"<email id='1' title='2222 — 2222222 ",后面的 "'testtest"全都不见了。实在是不知道什么原因,是不是特殊字符把后面的字符都省掉了阿,可是横杠在中间阿?
有哪位遇到过这样的情况吗?
解决办法倒是有,直接把后面去了:
 do until rec.eof
  stitle = rec("title")
  stitle = replace(stitle,"&","&amp;")
  stitle = replace(stitle,"'","&apos;")
  stitle = replace(stitle,"""","&quot;")
   str = "<email id='" & rec("ID") & "' title='" & stitle 
  response.write  str
 
   response.write "' projectid='" & rec("projectId") & "' />" & chr(10)  //把单引号加这就没事了
  rec.movenext
 loop

可是我很想知道为什么会发生这种情况,还请各位高手赐教!
<% sql="select a.id,a.title,b.projectId from tblIBBemail a" & _ " inner join tblibbautoemailcategory b on a.id = b.emailid" & _ " order by b.projectid,a.Title" set rec=server.createobject("ADODB.Recordset") rec.open sql, conn, 3, 3 if not rec.eof then do until rec.eof stitle = rec("title") stitle = replace(stitle,"&","&") stitle = replace(stitle,"'","'") stitle = replace(stitle,"""",""") response.write "" & chr(10) rec.movenext loop end if %> <% sql="select a.id,a.title,b.projectId from tblIBBemail a" & _ " inner join tblibbautoemailcategory b on a.id = b.emailid" & _ " order by b.projectid,a.Title" set rec=server.createobject("ADODB.Recordset") rec.open sql, conn, 3, 3 if not rec.eof then do until rec.eof stitle = rec("title") stitle = replace(stitle,"&","&") stitle = replace(stitle,"'","'") stitle = replace(stitle,"""",""") response.write "" & chr(10) rec.movenext loop end if %>