Home > Software engineering >  Adding Root Node into response result XML using VBS
Adding Root Node into response result XML using VBS

Time:09-05

I need to add root element to response result xml. We are receiving xml result from another(2nd) application which is not having proper root element hence our(1st) application unable read "RETURN_CODE" whether it is success or not.

Response from 2nd application:

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

Result XML from 2nd app : I have mentioned in the comment

Now I need to load this this result into xml but i am unable because result is not containing root element. Error while loading xml "Only one top level element is allowed in an XML document"

'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

I need help to load the response result(sResult) into Load xml (xmlResult). I save the xml results but nothing inside xml.

CodePudding user response:

Could you use the vbs replace function, eg

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

A couple of other points. First, why are you using Response.ContentType = "text/html" rather than text/xml? Second, it's almost never a good idea to use On Error Resume Next.

CodePudding user response:

Using XML Linq

using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApp2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.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);

        }

    }

}
 
  • Related