我正在构建一个服务,并且有一个客户端需要我尝试在我的肥皂服务中接收的特定格式的 xml。我遇到的问题是,当我需要仅在根节点上时,命名空间前缀应用于子节点。下面是在soapui中为请求生成的soap信封:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:contact.com">
<soapenv:Header/>
<soapenv:Body>
<urn:ContactNotification>
<!--Optional:-->
<urn:Identifier>
<!--Optional:-->
<ID>122</ID>
</urn:Identifier>
<!--Optional:-->
<urn:Details>
<!--Optional:-->
<ContactPerson>c</ContactPerson>
</urn:Details>
</urn:ContactNotification>
</soapenv:Body>
</soapenv:Envelope>
我需要保留前缀但将其从中删除 and 所以信封应该是这样的:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:contact.com">
<soapenv:Header/>
<soapenv:Body>
<urn:ContactNotification>
<!--Optional:-->
<Identifier>
<!--Optional:-->
<ID>122</ID>
</Identifier>
<!--Optional:-->
<Details>
<!--Optional:-->
<ContactPerson>c</ContactPerson>
</Details>
</urn:ContactNotification>
</soapenv:Body>
</soapenv:Envelope>
我需要在 WCF 中使用 XML Serializer 因为需要支持 xml 属性(上面的示例是精简版本)
这是我的课程的代码:
using System.ServiceModel;
[System.Xml.Serialization.XmlTypeAttribute (Namespace="")]
public partial class Identifier
{
private string idField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="")]
public string ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
using System.ServiceModel;
[System.Xml.Serialization.XmlType(Namespace ="")]
public partial class Details
{
private string contactPersonField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="")]
public string ContactPerson
{
get
{
return this.contactPersonField;
}
set
{
this.contactPersonField = value;
}
}
}
这些是我的服务接口/实现:
using System.Web.Configuration;
using System.Xml.Serialization;
namespace ContactService
{
[ServiceContract(Namespace = "urn:contact.com")]
[XmlSerializerFormat]
public interface IContactService
{
[OperationContract]
[XmlSerializerFormat]
void ContactNotification( Identifier Identifier, Details Details);
}
}
using System.ServiceModel;
using System.Web.Configuration;
using System.Xml;
using System.Xml.Serialization;
namespace ContactService
{
[ServiceBehavior(Namespace= "urn:contact.com")]
[XmlSerializerFormat]
public class ContactService : IContactService
{
[OperationBehavior]
public void ContactNotification(Identifier Identifer, Details Details)
{
}
}
已四处寻找但找不到解决方案。