none
Return Dataset data as a XML file from WCF service RRS feed

  • Question

  • Hi,

    I have a requirement something like:

    1. Write WCF service method and return XML file

    2. Method reads the data from datasource (SQL) and gives data in dataset

    4.  Convert the dataset data into XML file

    3. Return this XML file from the WCF service method

    4. Client appliaction should be able to downlaod xml file(dataset data) when request this method.

    I have been searching and figured out that multiple ways do like Streaming and REST service. What would be best approach in this scenarion.

    Help is appreciated.


    Thanks, Ram


    Friday, May 17, 2013 7:41 AM

Answers

  • Hi,

    If you want to return raw XML content in a WCF service operation, since .net DataTable is fully compatible and convertible to XML format, we can convert the DataTable instance into an XML document and return it as raw XML data. Below is an example.

    // OperationContract definition for operation that
    //returns a DataTable as raw XML content
    [OperationContract]
    XElement GetDataTableAsXML();
    // Implementation of the GetDataTableAsXML //OperationContract
    public System.Xml.Linq.XElement GetDataTableAsXML()
    {
    DataTable dt = GetTestDataTable();
    StringWriter strwtr = new StringWriter();
    dt.WriteXml(strwtr);
    return XElement.Parse(strwtr.ToString());
    }

    Also check:

    http://msdn.microsoft.com/en-us/library/system.data.datatable.writexml.aspx

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 20, 2013 5:53 AM
    Moderator

All replies

  • Hi,

    If you want to return raw XML content in a WCF service operation, since .net DataTable is fully compatible and convertible to XML format, we can convert the DataTable instance into an XML document and return it as raw XML data. Below is an example.

    // OperationContract definition for operation that
    //returns a DataTable as raw XML content
    [OperationContract]
    XElement GetDataTableAsXML();
    // Implementation of the GetDataTableAsXML //OperationContract
    public System.Xml.Linq.XElement GetDataTableAsXML()
    {
    DataTable dt = GetTestDataTable();
    StringWriter strwtr = new StringWriter();
    dt.WriteXml(strwtr);
    return XElement.Parse(strwtr.ToString());
    }

    Also check:

    http://msdn.microsoft.com/en-us/library/system.data.datatable.writexml.aspx

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 20, 2013 5:53 AM
    Moderator
  • Thank you, Haixia. Your inputs are very helpful. However, is there any chance to return file(.xml) from wcf service. I mean, when client request for a partcular method from WCF service, it should give xml file and client should downlaods it into local computer.

    Our complete requirement was something like:

    1. Tool 'XYZ' calls our wcf and downloads the file into local computer

    2. Tool 'ABC' takes downloaded xml file and moves the data availble in XML into data store.


    Thanks, Ram

    Monday, May 20, 2013 9:20 AM
  • Hi,

    From that method, the return result is just a XML document. You can pass that value as the content of the download button or something. Refer:

    http://ctrlcvprogrammer.blogspot.com/2012/02/normal-0-false-false-false-en-us-x-none_18.html

    For more questions of this, I'd suggest you ask in asp.net forum to get better help, thanks for your understanding.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 21, 2013 2:59 AM
    Moderator