Remove xml namespaces from WCF restful response

前端 未结 9 1659
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 11:13

I am using WCF to return a plain old XML (POX) document to the caller. I am using the XML Serializer formatter to turn the objects into XML.

In the returned docum

相关标签:
9条回答
  • 2020-12-05 11:38

    You can remove the XML namespace by setting the Namespace parameter of the DataContract attribute to an empty string, like so:

    [DataContract(Namespace = "")]
    public class ResponseInfo
    {
        // ...
    }
    

    I hope this helps...

    0 讨论(0)
  • 2020-12-05 11:38

    I have the same problem when I work with ASMX clients and For me this solve the problem :

    Add to your Service Interface :

    [XmlSerializerFormat(Use = OperationFormatUse.Literal, Style = OperationFormatStyle.Document)]
    

    Add to Operations :

    [OperationContract(Action = "http://www.YourNameSpace.com/ActionName",ReplyAction = "http://www.YourNameSpace.com/ActionName")]
    
    0 讨论(0)
  • 2020-12-05 11:41

    Not sure if this will help, but we had a similar issue. Instead of decorating thousands of data elements with DataContract/DataMember attributes and using the (default) DataContractSerializer, we found that if our WCF service used the XmlSerializerFormat instead, we could easily deserialize our objects.

    [System.ServiceModel.ServiceContract]
    public interface IRestService
    {
        [System.ServiceModel.OperationContract]
        // Added this attribute to use XmlSerializer instead of DataContractSerializer
        [System.ServiceModel.XmlSerializerFormat(
            Style=System.ServiceModel.OperationFormatStyle.Document)]
        [System.ServiceModel.Web.WebGet(
            ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Xml,
            UriTemplate = "xml/objects/{myObjectIdentifier}")]
        MyObject GetMyObject(int myObjectIdentifier);
    }
    

    This is how we're deserializing the objects:

    public static T DeserializeTypedObjectFromXmlString<T>(string input)
    {
        T result;
    
        try
        {
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
            using (System.IO.TextReader textReader = new System.IO.StringReader(input))
            {
                result = (T)xs.Deserialize(textReader);
            }
        }
        catch
        {
            throw;
        }
    
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题