添加根的xmlns XmlSerializer的使用与WCF SOAP [英] Adding Root xmlns Using XmlSerializer with WCF SOAP

查看:726
本文介绍了添加根的xmlns XmlSerializer的使用与WCF SOAP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我们的客户从第三方提供的XML Schema文档生成的类文件。我应该能够使用这个生成的类客户的SOAP的Web服务,但我有一些问题。

I have a class file generated from an XML Schema document supplied from a third party by our customer. I should be able to use this generated class to the customer's SOAP web service, but I'm having some problems.

我创建了一个的ServiceContract 接口,这样我就可以使用WCF 的ChannelFactory 来连接到Web服务,如如下:

I've created a ServiceContract interface so I can use the WCF ChannelFactory to connect to the web service, like follows:

[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 的重要位:

Both EPSStatus and DataEventSet are in my generated class file. The important bits of 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跟踪):

When I now try to call IWebService.serviceNotifyDataEventSet I get the following SOAP body (found with WCF Trace enabled on their server):

<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服务,它看起来好像我的数据是否正确连载然而在服务器端快到了空。我也得到了来自客户端的跟踪,它使用了以下机身工作:

So, I'm able to call the web service and it appears as though my data is serialising properly, however on the server side dataSet is coming up null. I've also got a trace from a client that does work with the following body:

<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中的xmlns =http://www.thirdparty.org/thirdapp> 。在我的包,而不是在所有指定的命名空间。

The only difference I can see is that the root namespace is set on the dataSet on the working packet: <dataSet xmlns="http://www.thirdparty.org/thirdapp">. On my packet, the namespace is not specified at all.

我的问题是,我的分析听起来合理,如果是这样,有什么办法可以得到根本的xmlns要正确输出上我的

My question is, does my analysis sound reasonable and if so, is there any way I can get the root xmlns to output properly on my dataSet?

推荐答案

我现在已经管理得到这个使用相对简单的方法工作。幸运的是, XSD 从XML架构生成的代码,标志着所有类偏,没有构造函数。我已经添加了我自己的部分类来定义覆盖命名空间的默认构造函数,如下所示:

I've now managed to get this to work using a relatively straightforward approach. Fortunately, the code generated from the XML Schemas by xsd marks all the classes as partial with no constructors. I've added my own partial class to define a default constructor that overrides the namespaces, as follows:

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>

这篇关于添加根的xmlns XmlSerializer的使用与WCF SOAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆