vbs 变化 <&lt;和我的 >&gt;导入文件内容时 [英] vbs changes < to &lt; and my > to &gt; when importing file contents
问题描述
我完全不知道什么时候使用 VBScript,所以当我弗兰肯斯坦化了两个在网上找到的简单代码片段以将文本文件的全部内容插入到 XML 文档中时,我感到非常高兴.
I have absolutly no idea when it comes to VBScript so I was quite happy when I frankensteined two simple code snippets found online to insert the entire contents of a text file into a XML document.
除了我的 <
已更改为 <
并且我的 <
已更改为 >
.
All works well except my <
have changed to <
and my <
has changed to >
.
我怎样才能克服这个问题?
How can I overcome this?
我的代码:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\test.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("C:\Audits.xml")
Set colNodes = xmlDoc.SelectNodes("/TOOLS")
For Each objNode In colNodes
objNode.text = (strText)
Next
xmlDoc.Save "C:\Audits.xml"
推荐答案
CDATA 部分允许 <和 >:
A CDATA section allows < and >:
CDATA 部分允许开发人员包含标记字符 <、>、和 &在元素内容中不使用字符或实体参考.脚本、样式表、程序代码和示例 XML 代码经常包含在 CDATA 部分中.
CDATA sections allow developers to include the markup characters <, >, and & within element content without using character or entity references. Scripts, style sheets, program code, and sample XML code are frequently contained in CDATA sections.
代码:
Dim oXML : Set oXML = CreateObject("Msxml2.DOMDocument")
Set oXML.documentElement = oXML.createElement("a")
Dim nd
Set nd = oXML.createElement("b")
nd.appendChild oXML.createTextNode("<>")
oXML.documentElement.appendChild nd
Set nd = oXML.createElement("c")
nd.appendChild oXML.createCDATASection("<>")
oXML.documentElement.appendChild nd
输出:
<a><b><></b><c><![CDATA[<>]]></c></a>
这篇关于vbs 变化 <&lt;和我的 >&gt;导入文件内容时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!