我有一个由我们的客户从第三方提供的
XML Schema文档生成的类文件.我应该能够将这个生成的类用于客户的SOAP Web服务,但是我遇到了一些问题.
我已经创建了一个ServiceContract接口,因此我可以使用WCF ChannelFactory连接到Web服务,如下所示:
[ServiceContract(Namespace = "http://theircompany.co.uk/theirapp/v1")] [XmlSerializerFormat] public interface IWebService { [OperationContract] EPsstatus serviceNotifyDataEventSet( [XmlElement(Namespace = "http://www.thirdparty.org/thirdapp")] DataEventSet dataSet ); }
EPsstatus和DataEventSet都在我生成的类文件中. DataEventSet的重要部分:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","2.0.50727.42")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.thirdparty.org/thirdapp")] [System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.thirdparty.org/thirdapp",IsNullable=false)] public partial class DataEventSet { //... }
当我现在尝试调用IWebService.serviceNotifyDataEventSet时,我得到以下SOAP主体(在其服务器上启用了WCF跟踪):
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <serviceNotifyDataEventSet xmlns="http://theircompany.co.uk/theirapp/v1"> <dataSet> <dataEvents xsi:type="q1:DAInt" xmlns="" xmlns:q1="http://www.thirdparty.org/thirdapp"> <id>47245361157</id> <time> <tick_time>141728877218</tick_time> <time>2012-06-28T10:07:57.218+01:00</time> <time_type>OSACBM_TIME_MIMOSA</time_type> </time> <value>42</value> </dataEvents> <id xmlns="">0</id> <site xmlns=""> <category>SITE_SPECIFIC</category> </site> <time xmlns=""> <tick_time>141728877218</tick_time> <time>2012-06-28T10:07:57.218+01:00</time> <time_type>OSACBM_TIME_MIMOSA</time_type> </time> </dataSet> </serviceNotifyDataEventSet> </s:Body>
所以,我能够调用Web服务,看起来好像我的数据正确序列化,但是在服务器端,dataSet会出现空值.我也从一个与以下机构合作的客户端得到了一个跟踪:
<soap:Body> <serviceNotifyDataEventSet xmlns="http://theircompany.co.uk/theirapp/v1"> <dataSet xmlns="http://www.thirdparty.org/thirdapp"> <dataEvents xmlns:q1="http://www.thirdparty.org/thirdapp" xsi:type="q1:DAReal" xmlns=""> <id>47245361408</id> <time> <tick_time>141730618844</tick_time> <time>2012-06-28T10:36:58.843+01:00</time> <time_type>OSACBM_TIME_MIMOSA</time_type> </time> <value>12.34</value> </dataEvents> <id xmlns="">0</id> <site xmlns=""> <category>SITE_SPECIFIC</category> </site> <time xmlns=""> <tick_time>141730618843</tick_time> <time>2012-06-28T10:36:58.843+01:00</time> <time_type>OSACBM_TIME_MIMOSA</time_type> </time> </dataSet> </serviceNotifyDataEventSet> </soap:Body>
我能看到的唯一区别是根名称空间是在工作包上的dataSet上设置的:< dataSet xmlns =“http://www.thirdparty.org/thirdapp”>.在我的数据包上,根本没有指定名称空间.
我的问题是,我的分析听起来是否合理,如果是这样,有什么办法可以让我的数据集上的根xml输出正确吗?
解决方法
我现在已经设法使用相对简单的方法来实现这一点.幸运的是,xsd从XML Schema生成的代码将所有类标记为部分没有构造函数.我添加了自己的部分类来定义覆盖命名空间的默认构造函数,如下所示:
public partial class DataEventSet { [XmlNamespaceDeclarations] public XmlSerializerNamespaces _xmlns; /// <summary> /// Constructor for DataEventSet that sets up default namespaces /// </summary> public DataEventSet() { _xmlns = new XmlSerializerNamespaces(); _xmlns.Add("","http://www.thirdparty.org/thirdapp"); _xmlns.Add("o","http://www.thirdparty.org/thirdapp"); } }
现在序列如下:
<?xml version="1.0" encoding="utf-8"?> <s:Body xmlns:s="http://www.w3.org/2003/05/soap-envelope"> <serviceNotifyDataEventSet xmlns="http://theircompany.co.uk/theirapp/v1"> <dataSet xmlns="http://www.thirdparty.org/thirdapp" xmlns:o="http://www.thirdparty.org/thirdapp"> <dataEvents xsi:type="o:DABool" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <id>47245361157</id> <value>true</value> </dataEvents> <id xmlns="">0</id> <site xmlns=""> <category>SITE_SPECIFIC</category> </site> <time xmlns=""> <tick_time>396106152171</tick_time> <time>2012-07-20T13:29:12.171Z</time> <time_type>OSACBM_TIME_MIMOSA</time_type> </time> </dataSet> </serviceNotifyDataEventSet> </s:Body>