问题
I am building a service and have a client that requires a specific format of xml that I am trying to receive in my soap service. The issue I have is that the namespace prefix is applying to child nodes when I need it to be only on the root node. Below is the soap envelope that is generated for request in soapui:
<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>
I need to keep the prefix on < urn:ContactNotification > but remove it from < urn:Identifier > and < urn:Details > so the envelope should look like:
<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>
I need to use the XML Serializer in WCF as need to support xml attributes (example above is cut down version)
Here is the code for my classes:
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;
}
}
}
And these are my service Interface/Implementation:
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)
{
}
}
Have hunted around but cannot find a solution for this.
来源:https://stackoverflow.com/questions/41291500/wcf-soap-remove-namespace-from-child-nodes