locked
How to return objects in XML format in WCF service RRS feed

  • Question

  • Hi

    I have a very simple WCF web service, and define a simple class, "Employee". there is a method, GetActiveEmployees(), I'd like the method return XML format employees, so client call the method and then get XML, not object.

    Is there any simple way to do it?

    [DataContract]

    class Employee{

    public FirstName{ get; set;}

    public LastName{get;set;}

    }

    thanks.

    Friday, October 15, 2010 3:34 AM

Answers

  • Hi George,

    If you want to transfer XML format data(raw XML) in WCF or ASMX webservice operations, you can define the parameter or return value of  XmlElement or XElement(LINQ to XML type) type.

    Or if you're developing WCF REST service, you can define the service operation to use raw programming model and return arbitrary data format via Stream object.

    #Creating WCF Service for xml request and xml response
    http://social.msdn.microsoft.com/Forums/en/wcf/thread/b082d6de-d1e9-4e51-a0ab-0fe98d7003e6


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, October 18, 2010 6:05 AM

All replies

  • On 10/14/2010 11:34 PM, George Dong wrote:
    > Hi
    >
    > I have a very simple WCF web service, and define a simple class,
    > "Employee". there is a method, GetActiveEmployees(), I'd like the method
    > return XML format employees, so client call the method and then get XML,
    > not object.
    >
    > Is there any simple way to do it?
    >
    > [DataContract]
    >
    > class Employee{
    >
    > public FirstName{ get; set;}
    >
    > public LastName{get;set;}
    >
    > }
    >
    > thanks.
    >
     
    Then use an XML Document object with attributes, tags and data and send
    string. You parse the XML document to a string variable and send it as
    string. You take the string data and load a XML Document with string
    data, which puts it back into XML.
     
    Use Bing or Google and look it up.
     
    Friday, October 15, 2010 3:50 AM
  • You can use DataContractSerializer to serialize your object to xml.

    DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));
          MemoryStream ms = new MemoryStream();
          
          dcs.WriteObject(ms, new Employee() { FirstName = "fn", LastName = "ln" });
          ms.Position = 0;
    
          string s = new StreamReader(ms).ReadToEnd();  // xml result
    
    Friday, October 15, 2010 8:04 AM
  • Hi George,

    If you want to transfer XML format data(raw XML) in WCF or ASMX webservice operations, you can define the parameter or return value of  XmlElement or XElement(LINQ to XML type) type.

    Or if you're developing WCF REST service, you can define the service operation to use raw programming model and return arbitrary data format via Stream object.

    #Creating WCF Service for xml request and xml response
    http://social.msdn.microsoft.com/Forums/en/wcf/thread/b082d6de-d1e9-4e51-a0ab-0fe98d7003e6


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, October 18, 2010 6:05 AM