繁体   English   中英

在没有WSDL的情况下从webservice反序列化SOAP响应

[英]Deserialize SOAP response from webservice without WSDL

我正在尝试编写一个可以从Web服务反序列化SOAP响应的C#.NET应用程序。 Web服务(此处称为“Wibble”)没有WSDL(Grrrrrrr)。 我有一个完整的样本响应的副本,我相信我可以使用它来生成中间类,但尽管尝试了许多不同的方法,我无法从响应中获得一个合理的对象。

响应的前几行如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns1:inspectResponse xmlns:ns1="ProjectService" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <inspectReturn href="#id0"/>
        </ns1:inspectResponse>
        <multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="Wibble" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Project">
            <category xsi:type="ns2:Category" xsi:nil="true"/>
            <classId xsi:type="xsd:long">1000000</classId>
            [...]
        </multiRef>
        <multiRef xmlns:ns3="Wibble" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id3" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:ProjectData">
            <author xsi:type="ns3:User" xsi:nil="true" />
            <authorUserId xsi:type="xsd:long">5289027</authorUserId>
            <classId xsi:type="xsd:long">0</classId>
            <comments xsi:type="xsd:string">Some comments.</comments>
            [...]        
        </multiRef>
    </soapenv:Body>
</soapenv:Envelope>

等等...

首先,如果我尝试使用这样的SoapFormatter

var formatter = new SoapFormatter();
var blah = formatter.Deserialize(memstream);
return blah.ToString();

我得到一个SerializationExceptionParse Error, no assembly associated with Xml key ns1 inspectResponse

所以我猜它缺少一个名为inspectResponse的类,它可以将第一个元素映射到。 所以我破解xsd.exe并从XML文件生成一些xsds。 从这里开始,我生成一个52KB的C#类,其中包含一大堆代码(我猜)它包含了XML文件可以映射到的所有类。 我包括它,并重新运行上面的代码,并得到完全相同的错误。

所以我得到的想法是,现在我有自动生成的类,我可以使用XmlSerializer对象并尝试反序列化。 我写这个:

var ss = new XmlSerializer(typeof(Classes.Envelope));
object blah;
using (var xr = XmlReader.Create(new StringReader(response)))
{
    ss.Deserialize(xr);
    blah = ss.Deserialize(xr);
}
return blah.ToString();

这次我得到一个新的InvalidOperationExceptionThere is an error in XML document (2, 356). ---> System.InvalidOperationException: The specified type was not recognized: name='Project', namespace='Wibble', at <multiRef xmlns=''>. There is an error in XML document (2, 356). ---> System.InvalidOperationException: The specified type was not recognized: name='Project', namespace='Wibble', at <multiRef xmlns=''>.

自动生成的代码不包含Project类,但它包含multiRef类。 据推测,这是因为没有Project类存在。 我尝试创建一个占位符:

[Serializable]
[XmlType(TypeName = "Project", Namespace = "Wibble")]
public class Project
{
}

但那没有效果。

我是不是在这里,或者我只是错过了一些小东西? 我很欣赏它是一个相当复杂的XML响应,它有多个不同类型的multiRef元素,但我希望SoapSerializer能够用它做一些事情。

好的,为什么不尝试将响应加载到XMLDomDocument中并使用XPATH表达式或XSLT来查询您感兴趣的节点。然后,自定义映射器类可以将节点属性映射到POCO类中。

如果您没有找到任何其他内容,只是一个想法和一个糟糕的工作,但我很惊讶SOAP返回消息没有WSDL。 这是一项公共服务吗?

您是否还尝试在服务URL的末尾追加?WSDL,看看会发生什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM