C# 使用带有WCF SOAP的XmlSerializer添加根xmlns
我有一个类文件,它是由客户从第三方提供的XML模式文档生成的。我应该能够将这个生成的类用于客户的soapweb服务,但是我遇到了一些问题 我已经创建了一个C# 使用带有WCF SOAP的XmlSerializer添加根xmlns,c#,xml,wcf,soap,xsd,C#,Xml,Wcf,Soap,Xsd,我有一个类文件,它是由客户从第三方提供的XML模式文档生成的。我应该能够将这个生成的类用于客户的soapweb服务,但是我遇到了一些问题 我已经创建了一个ServiceContract接口,因此我可以使用WCFChannelFactory连接到web服务,如下所示: [ServiceContract(Namespace = "http://theircompany.co.uk/theirapp/v1")] [XmlSerializerFormat] public interface IWebSe
ServiceContract
接口,因此我可以使用WCFChannelFactory
连接到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
);
}
epstatus
和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跟踪):
我能看到的唯一区别是根命名空间是在工作包的
数据集上设置的。在我的数据包上,根本没有指定名称空间
我的问题是,我的分析听起来合理吗?如果合理,是否有任何方法可以让根xmlns在我的
数据集上正确输出?您的分析听起来合理。在查看您发布的代码时,我怀疑DataEventSet
类是否是您应该查看的与
元素相关的类。使用System.Xml.Serialization.XmlRootAttribute
应该允许您为元素定义/应用正确的命名空间。我的猜测是,您需要在另一个类上使用此属性来正确输出
元素。我现在已经使用一种相对简单的方法实现了这一点。幸运的是,xsd
从XML模式生成的代码将所有类标记为不带构造函数的部分类。我添加了自己的分部类来定义覆盖名称空间的默认构造函数,如下所示:
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>
公共部分类DataEventSet
{
[xmlnamespace声明]
公共多媒体空间;
///
///用于设置默认命名空间的DataEventSet的构造函数
///
公共数据事件集()
{
_xmlns=新的XmlSerializerNamespaces();
_添加(“,”http://www.thirdparty.org/thirdapp");
_添加(“o”http://www.thirdparty.org/thirdapp");
}
}
本文件现在按如下顺序连续发表:
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>
47245361157
真的
0
特定地点
396106152171
2012-07-20T13:29:12.171Z
含羞草