在xml根标记中明确添加名称空间WCF

时间:2017-04-06 09:17:28

标签: xml wcf soapui

我创建了一个WCF服务,我使用SOAPUI发送SOAP请求。

以下是该工具从我的C#模型生成的Request xml:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:w3="http://www.w3.org/">
    <soapenv:Header />
    <soapenv:Body>
        <w3:PerformScan>
            <w3:request>
                <w3:SearchConfiguration>
                    <w3:ConfidenceThreshold>?</w3:ConfidenceThreshold>
                    <w3:ResultConfiguration></w3:ResultConfiguration>
                    <w3:ScanRequest xsi:type="w3:CustomerRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                        <w3:CustomerId>?</w3:CustomerId>
                        <w3:CustomerName>?</w3:CustomerName>
                    </w3:ScanRequest>
                </w3:SearchConfiguration>
            </w3:request>
        </w3:PerformScan>
    </soapenv:Body>
</soapenv:Envelope>

但为了使其可行,我需要修改请求xml。下面是修改后的xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:w3="http://www.w3.org/">
    <soapenv:Header />
    <soapenv:Body>
        <w3:PerformScan xmlns:w3="http://www.w3.org/">
            <w3:request>
                <w3:SearchConfiguration>
                    <w3:ConfidenceThreshold>?</w3:ConfidenceThreshold>
                    <w3:ResultConfiguration></w3:ResultConfiguration>
                    <w3:ScanRequest xsi:type="w3:CustomerRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                        <w3:CustomerId>?</w3:CustomerId>
                        <w3:CustomerName>?</w3:CustomerName>
                    </w3:ScanRequest>
                </w3:SearchConfiguration>
            </w3:request>
        </w3:PerformScan>
    </soapenv:Body>
</soapenv:Envelope>

如何在从工具生成时自动填充xmlns:w3="http://www.w3.org/"节点中的c#模型或如何使PerformScan自动填充。

1 个答案:

答案 0 :(得分:0)

了解这是您的申请的问题 可能在脚本之下会帮助您,直到您在应用程序中获得修复。

以下是如何在soapUI中完成的:

  • 创建测试用例
  • 添加groovy script测试步骤,例如步骤1。
  • 添加soap request测试步骤,比如step2。与您目前拥有的要转换的请求相同。

下面的脚本进入第1步,它会根据需要在步骤2中更新请求。

Groovy脚本:这使用固定的xml。

import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep

def setRequest = { request ->
    def step = context.testCase.testStepList[context.currentStepIndex+1]
    if (step instanceof WsdlTestRequestStep) {
        step.testRequest.requestContent = request 
        log.info "Request for ${step.name} step is updated"
    }
}

def xml = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:w3="http://www.w3.org/">
<soapenv:Header />
<soapenv:Body>
    <w3:PerformScan>
        <w3:request>
            <w3:SearchConfiguration>
                <w3:ConfidenceThreshold>?</w3:ConfidenceThreshold>
                <w3:ResultConfiguration></w3:ResultConfiguration>
                <w3:ScanRequest xsi:type="w3:CustomerRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <w3:CustomerId>?</w3:CustomerId>
                    <w3:CustomerName>?</w3:CustomerName>
                </w3:ScanRequest>
            </w3:SearchConfiguration>
        </w3:request>
    </w3:PerformScan>
</soapenv:Body>
</soapenv:Envelope>
"""
setRequest(groovy.xml.XmlUtil.serialize(new XmlSlurper().parseText(xml)))

如果您想使用动态xml,即从步骤2中读取请求并将更改后的xml设置回来,请使用以下脚本而不是上面的脚本:

Groovy脚本:

import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep

def getNextStep = { context.testCase.testStepList[context.currentStepIndex+1] }

def getRequest = { step ->      
        if (step instanceof WsdlTestRequestStep) {
            log.info "Getting request for ${step.name}"
            return step.testRequest.requestContent
        }
        null
}

def setRequest = { step, request ->
    if (step instanceof WsdlTestRequestStep) {
        step.testRequest.requestContent = request 
        log.info "Request for ${step.name} step is updated"
    }
}
def xml = getRequest(getNextStep())
if (xml) {
    setRequest(getNextStep(), groovy.xml.XmlUtil.serialize(new XmlSlurper().parseText(xml)))    
}

您可以在线快速尝试 Demo