none
How to return XML from WCF Service and Consuming from the client RRS feed

  • Question

  • Hi,

    I have the requirement like I need to get set of records from the Business Layer to my WCF service.

    Now I need to make one XML and pass all the received records to my Client in the form of XML. The client application could be developed in PHP or Java.

    Could anyone please let me know how could I achieve this.

    It would be great if you give some suggession or idea and How to make an XML and how to parse it in the client side.

    Thanks,
    Raja
    Monday, August 24, 2009 12:26 PM

Answers

All replies

  • Hi Raja,

    you could create a RESTful service and return XML data easily. Look here for more info on the subject. Regarding the client side, there will be no problems, XML is interoperable and there are Java and PHP APIs that will do the job for you.

    Regards,
    Stipe-Ivan
    Monday, August 24, 2009 12:58 PM
  • look at the WCF Raw programming model sample by Carlos located at http://blogs.msdn.com/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx


    Amit Sharma
    Monday, August 24, 2009 4:32 PM
    Moderator
  • Hi,

    Thaks for the reply.

    I need to return XML from WCF Service to Client.

    Please find the below code


    // Service file

     

    public class Service : IService

    {

      private XmlDocument GetXML(DataTable objDT)

      {

         XmlDocument objXMLDoc = new XmlDocument();

         objXMLDoc.LoadXml(objDT.DataSet.GetXml());

         return objXMLDoc;

      }

     

     

      public XmlDocument GetStudentInfo2(int iStudentID, string strClientAcc)

      {

    try

    {

      clsStudent objStudent = new clsStudent();

      // I am calling my Business layer method and it will return datatable

     

     DataTable objDt = objStudent.GetStudentInfo2(iStudentID,strClientAcc);

      return GetXML(objDt);

          }

    catch (Exception ex)

    {

     throw ex;

    }

      }

    }

    // Interface

    [ServiceContract]

    public interface IService

    {

      [OperationContract]

      XmlDocument GetStudentInfo2(int iStudentID, string strClientAcc);

    }


    When I run the Service I am getting the below error. As I beleive that we need to give the return type should be Serializable.

    Type 'System.Xml.XmlDocument' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  See the Microsoft .NET Framework documentation for other supported types.

    Could you please let me know how to solve this issue and how could I return my data in the form of XML.

    Thanks,
    Raja

     

    Tuesday, August 25, 2009 7:55 AM
  • Hi Raja,

    Because you need to write the Java/PHP client for WCF service, our reply recommend that you use REST style to create WCF service. Any client can issue Http request and parse plain XML text in this scenario. Please have a look at the following links:

    1.  WCF REST Programming Model
    http://msdn.microsoft.com/en-us/library/bb412169.aspx
    You can find the feature in detail from this section.

    2. An Introduction To RESTful Services With WCF
    http://msdn.microsoft.com/en-us/magazine/dd315413.aspx
    It discusses some of the basic tenets of REST, as well as present an implementation of a RESTful service using WCF.
    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, August 26, 2009 8:31 AM
    Moderator
  • Hi Raja,

    In your code, you use SOAP(Simple Object Access Protocol) between WCF service and client application. WCF Runtime will serialize/deserialize data between .NET object and XML text for interoperability. The above errors means that XmlDocument can't be serialized in this scenario. You can use String type variable in this scenario between Service and client in this scenario.

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, August 26, 2009 8:38 AM
    Moderator
  • I think this post can help you
    http://forums.asp.net/p/1449957/3308310.aspx
    Wednesday, August 26, 2009 10:08 AM
  • <XmlSerializerFormat()> _
    <ServiceContract()> _
    Public Interface IService

    Will do the trick

    • Proposed as answer by Ateş DANIŞ Saturday, October 20, 2012 6:40 AM
    Thursday, September 15, 2011 8:30 AM