Remove xml namespaces from WCF restful response

前端 未结 9 1662
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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(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;
    }
    

提交回复
热议问题