vbscript创建带有特殊字符的转换xml

vbscript创建带有特殊字符的转换xml,xml,vbscript,Xml,Vbscript,我正在.vbs文件中创建一个xml文件,其节点值如下所示 <car>David's</car> <company>Mannar & Co.</company> David's 曼纳公司。 在解析此xml时,我发现了与&等有关的问题 我想将所有可能的xml特殊字符转换为编码字符(使用函数或其他东西),以便在解析时获得原始内容 谢谢。根据OP的评论,我自己制作的版本,找不到可靠的版本,我想它涵盖了所有可能的ascii字符 Funct

我正在.vbs文件中创建一个xml文件,其节点值如下所示

  <car>David's</car>
  <company>Mannar & Co.</company>
David's
曼纳公司。
在解析此xml时,我发现了与&等有关的问题

我想将所有可能的xml特殊字符转换为编码字符(使用函数或其他东西),以便在解析时获得原始内容


谢谢。

根据OP的评论,我自己制作的版本,找不到可靠的版本,我想它涵盖了所有可能的ascii字符

Function HTML_Encode(byVal string)
  Dim tmp, i 
  tmp = string
  For i = 160 to 255
    tmp = Replace(tmp, chr(i), "&#" & i & ";")
  Next
  tmp = Replace(tmp, chr(34), "&quot;")
  tmp = Replace(tmp, chr(39), "&apos;")
  tmp = Replace(tmp, chr(60), "&lt;")
  tmp = Replace(tmp, chr(62), "&gt;")
  tmp = Replace(tmp, chr(38), "&amp;")
  tmp = Replace(tmp, chr(32), "&nbsp;")
  HTML_Encode = tmp
End Function

Function HTML_Decode(byVal encodedstring)
  Dim tmp, i
  tmp = encodedstring
  tmp = Replace(tmp, "&quot;", chr(34) )
  tmp = Replace(tmp, "&apos;", chr(39))
  tmp = Replace(tmp, "&lt;"  , chr(60) )
  tmp = Replace(tmp, "&gt;"  , chr(62) )
  tmp = Replace(tmp, "&amp;" , chr(38) )
  tmp = Replace(tmp, "&nbsp;", chr(32) )
  For i = 160 to 255
    tmp = Replace(tmp, "&#" & i & ";", chr(i))
  Next
  HTML_Decode = tmp
End Function

str = "This !@#± is a & test!"
wscript.echo HTML_Encode(str) '=> This&nbsp;!@#&amp;#177;&nbsp;is&nbsp;a&nbsp;&amp;&nbsp;test!
wscript.echo HTML_Decode(HTML_Encode(str)) '=> This !@#± is a & test!

当我找到另一把钥匙时,我的钥匙还没冷,我给出这个作为另一个答案,因为输出完全不同,所以你可以选择最适合的。我确实删除了原始答案,以避免混淆

Function Escape(s) 
  Dim scr
  Set scr = CreateObject("MSScriptControl.ScriptControl")
  scr.Language = "VBScript"
  scr.Reset
  Escape = scr.Eval("escape(""" & s & """)")
End Function

Function Unescape(s)
  Dim scr
  Set scr = CreateObject("MSScriptControl.ScriptControl")
  scr.Language = "VBScript"
  scr.Reset
  Unescape = scr.Eval("unescape(""" & s & """)")
End Function

wscript.echo Escape("This !@#± is a & test!") '=> This%20%21@%23%B1%20is%20a%20%26%20test%21
wscript.echo Unescape(Escape("This !@#± is a & test!")) '=> This !@#± is a & test!

这是一个老帖子,但我正在回复,因为我希望这能帮别人省去一些悲伤

我正在处理一个问题,其中一个供应商抱怨说,在某些情况下,并不是所有的特殊字符都在XML中转义。我惊讶地发现开发人员使用了自己的逻辑(函数)而不是框架提供的一些功能,因为转义听起来像是一项非常常见的任务。以下是修复之前的函数:

Function HTML_Encode(byVal string)
  Dim tmp, i 
  tmp = string
  For i = 160 to 255
    tmp = Replace(tmp, chr(i), "&#" & i & ";")
  Next
  tmp = Replace(tmp, chr(34), "&quot;")
  tmp = Replace(tmp, chr(39), "&apos;")
  tmp = Replace(tmp, chr(60), "&lt;")
  tmp = Replace(tmp, chr(62), "&gt;")
  tmp = Replace(tmp, chr(38), "&amp;") <- the problem: this line should be the first replacement
  tmp = Replace(tmp, chr(32), "&nbsp;")
  HTML_Encode = tmp
End Function

为了完整起见,您可以在XML中找到预定义的实体

Hi@itsraja,我发现其中一个答案有些不准确,并提出了一个修复方案。你现在可能会继续前进,但我正在更新你,因为这可能仍然是相关的(并且可以帮助其他人)。
  Function HTML_Encode(byVal string)
      Dim tmp, i 
      tmp = string

      tmp = Replace(tmp, chr(38), "&amp;") <- Must be the first replacement (Thanks Aaron)

      For i = 160 to 255
        tmp = Replace(tmp, chr(i), "&#" & i & ";")
      Next

      tmp = Replace(tmp, chr(34), "&quot;")
      tmp = Replace(tmp, chr(39), "&apos;")
      tmp = Replace(tmp, chr(60), "&lt;")
      tmp = Replace(tmp, chr(62), "&gt;")
      tmp = Replace(tmp, chr(32), "&nbsp;")
      HTML_Encode = tmp
    End Function