簡體   English   中英

使用 VBS 將根節點添加到響應結果 XML

[英]Adding Root Node into response result XML using VBS

我需要將根元素添加到響應結果 xml。 我們收到來自另一個(第二個)應用程序的 xml 結果,該應用程序沒有正確的根元素,因此我們的(第一個)應用程序無論是否成功都無法讀取“RETURN_CODE”。

第二次申請的回應:

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)加載到 Load xml (xmlResult) 中。 我保存了 xml 結果,但 xml 內沒有任何內容。

難道你不能只是連接你的根級 xml 標簽。 例如

sCallResult = "<rootname>" & vbcrlf
sCallResult = sCallResult & objRemoteServerPost.Retrieve(sUrl,Request_GET,"", "", "") & vbcrlf
sCallResult = sCallResult & "</rootname>"

其他幾點。 首先,您為什么使用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:\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);

        }

    }

}
 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM