如何将SOAP信封传递给需要Object的Web服务方法

时间:2017-03-17 14:40:31

标签: xml web-services soap

我在soap信封内传递一个XML字符串,web服务方法将一个对象作为参数,它的某个内部类与XML具有相同的结构,客户端不知道这个类。我需要从XML字符串填充对象。我正在使用HttpWebRequest来实现这一目标,但目前没有运气。

客户代码:

HttpWebRequest request = CreateWebRequest(url);
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><" + methodName + " xmlns='http://xsp.com/module'><MyClass><strXML xsi:type='xsd:string'>" + xml + "</strXML></MyClass></" + methodName + "></soap:Body></soap:Envelope>");
using (Stream stream = request.GetRequestStream())
{
    soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
    {
        string soapResult = rd.ReadToEnd();                    
    }
}

网络服务代码:

[WebMethod]
public string TestMethod(MyClass myClass)
{
    if (myClass == null)
        return "myClass is null";
    if (myClass.strXML == null)
    {               
        return "myclass.strXML is null";                
    }            
    return myClass.strXML.toString();
}

public class MyClass
{
    private object sXML;        
    public object strXML
    {
        get
        {
            return this.sXML;
        }
        set
        {
            this.sXML = value;
        }
    }        
}

任何帮助都将受到高度赞赏。

0 个答案:

没有答案