使用VBS将根节点添加到响应结果XML中



我需要将根元素添加到响应结果xml中。我们正在接收来自另一个(第二(应用程序的xml结果,该应用程序没有适当的根元素;返回代码";无论成功与否。

第二个应用程序的响应:

Set objRemoteServerPost = server.CreateObject("SOFT.ASPtear")
'AppendToLog("Before 2nd app call")
Response.ContentType = "text/html"
On Error Resume Next
'call 2nd Application to create workitem via get method
sCallResult = objRemoteServerPost.Retrieve(sUrl,Request_GET,"", "", "")

Set objRemoteServerPost = nothing

第二个应用程序的结果XML:我在评论中提到过

现在我需要将这个结果加载到xml中,但我无法加载,因为结果不包含根元素。加载xml时出错";在XML文档中只允许一个顶层元素";

'create xml object to retrieve return code  
set xmlResult= Server.CreateObject("Microsoft.XMLDOM")
'load result into xml
xmlResult.loadXML sCallResult
' retrieve return code
set elemReturnStatus=xmlResult.selectSingleNode("//RETURN_CODE")
if not (elemReturnStatus is nothing) then
' check if call was successful

我需要帮助将响应结果(sResult(加载到Loadxml(xmlResult(中。我保存了xml结果,但没有在xml中保存任何内容。

你能使用vbs替换函数吗,例如

sCallResult = replace(sCallResult, "<response>", "<root><response>")
sCallResult = replace(sCallResult, "</RETURN_CODE>", "</RETURN_CODE></root>")

还有几点。首先,为什么使用Response.ContentType = "text/html"而不是text/xml?其次,使用On Error Resume Next几乎从来都不是一个好主意。

使用XML Linq

using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApp2
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
string ident = "<?xml version = "1.0" ?><response>Successful</response>";
XDocument doc = XDocument.Parse(ident);
XElement response  = doc.Root;
XElement returnCode = new XElement("RETURN_CODE", 0);
response.Add(returnCode);
}
}
}

最新更新