none
. Net framework WCF service method to return the XML? RRS feed

Answers

  • Hi,

    I have created the following example for returning xml data using WCF Rest Service, please try to check it:
    In the IService1:

            [ServiceContract]
            public interface IService1
            {
                [OperationContract]
                [WebInvoke(Method = "GET",
                          ResponseFormat = WebMessageFormat.Xml,
                          UriTemplate = "data/{id}")]
                Person GetData(string id);
            }
        }
    
    
     [DataContract]
    public class Person
    {
            [DataMember]
            public int Id { get; set; }
               
            [DataMember]
            public string Name { get; set; }
    }
    


    In the Service1:

    public class Service1 : IService1
        {
    
            public Person GetData(string id)
            {
                return new Person()
                {
                    Id = Convert.ToInt32(id),
                    Name = "Leo Messi"
                };
            }
        }

    The result:

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, July 22, 2014 5:35 AM
    Moderator

All replies

  • This is the default behaviour of many kind of bindings provided by WCF as it's using SOAP by default.

    Is you are looking for REST services then in your service contract you can specify if you want to return XML or JSON. XML is the default and you need to change the ResponseFormat to JSON is so.

    See the following MSDN article for more info:

    http://msdn.microsoft.com/en-us/library/ee476510(v=vs.110).aspx

    Monday, July 21, 2014 9:51 AM
  • Hi,

    I have created the following example for returning xml data using WCF Rest Service, please try to check it:
    In the IService1:

            [ServiceContract]
            public interface IService1
            {
                [OperationContract]
                [WebInvoke(Method = "GET",
                          ResponseFormat = WebMessageFormat.Xml,
                          UriTemplate = "data/{id}")]
                Person GetData(string id);
            }
        }
    
    
     [DataContract]
    public class Person
    {
            [DataMember]
            public int Id { get; set; }
               
            [DataMember]
            public string Name { get; set; }
    }
    


    In the Service1:

    public class Service1 : IService1
        {
    
            public Person GetData(string id)
            {
                return new Person()
                {
                    Id = Convert.ToInt32(id),
                    Name = "Leo Messi"
                };
            }
        }

    The result:

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, July 22, 2014 5:35 AM
    Moderator