Solved

Convert Rest response to DataSet at client c#

Posted on 2013-12-01
1
2,124 Views
Last Modified: 2013-12-01
I'm returning a DataSet with my Rest Call. How can I read it back into a DataSet or JSON or XML at the client?

My server code is:
        public DataSet GetDataSet(string strSQL)
        {
            DataSet ds = new DataSet();
           //populate ds here
            return ds;
        }
my contract is:

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "MyHttpGetDataSet/{value}")]
        DataSet GetDataSet(string value);

my client code is:
            HttpClient client = new HttpClient();
            String url = System.Web.HttpUtility.HtmlEncode("http://localhost:3729/HttpService.svc/MyHttpGetDataSet/SELECT id FROM Table1");

            HttpResponseMessage wcfResponse = client.GetAsync(url).Result;
            HttpContent stream = wcfResponse.Content;
            var data = stream.ReadAsStringAsync();

Reading data.Return.ToString() shows:
<GetDataSetResponse xmlns=\"http://tempuri.org/\"><GetDataSetResult><xs:schema id=\"NewDataSet\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"><xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:UseCurrentLocale=\"true\"><xs:complexType><xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\"><xs:element name=\"Table1\"><xs:complexType><xs:sequence><xs:element name=\"Branch\" type=\"xs:string\" minOccurs=\"0\"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"><NewDataSet xmlns=\"\"><Table1 diffgr:id=\"Table11\" msdata:rowOrder=\"0\"><Branch>Wholesale Department</Branch></Table1><Table1 diffgr:id=\"Table12\" msdata:rowOrder=\"1\"><Branch>Brisbane Sales</Branch></Table1><Table1 diffgr:id=\"Table13\" msdata:rowOrder=\"2\"><Branch>Mackay Sales</Branch></Table1><Table1 diffgr:id=\"Table14\" msdata:rowOrder=\"3\"><Branch>Sydney Sales</Branch></Table1><Table1 diffgr:id=\"Table15\" msdata:rowOrder=\"4\"><Branch>Melbourne Sales</Branch></Table1><Table1 diffgr:id=\"Table16\" msdata:rowOrder=\"5\"><Branch>Adelaide Sales</Branch></Table1><Table1 diffgr:id=\"Table17\" msdata:rowOrder=\"6\"><Branch>AEA Head Office</Branch></Table1><Table1 diffgr:id=\"Table18\" msdata:rowOrder=\"7\"><Branch>Export Department</Branch></Table1><Table1 diffgr:id=\"Table19\" msdata:rowOrder=\"8\"><Branch>All Sales Branches</Branch></Table1><Table1 diffgr:id=\"Table110\" msdata:rowOrder=\"9\"><Branch>Brisbane Northside</Branch></Table1><Table1 diffgr:id=\"Table111\" msdata:rowOrder=\"10\"><Branch>Perth Sales</Branch></Table1><Table1 diffgr:id=\"Table112\" msdata:rowOrder=\"11\"><Branch>Rockhampton Sales</Branch></Table1></NewDataSet></diffgr:diffgram></GetDataSetResult></GetDataSetResponse>
0
Comment
Question by:Netlink2
1 Comment
 
LVL 1

Accepted Solution

by:
Netlink2 earned 0 total points
ID: 39688118
It wasn't easy, but I found it:
            HttpClient client = new HttpClient();
            String url = System.Web.HttpUtility.HtmlEncode("http://localhost:3729/HttpService.svc/MyHttpGetDataSet/SELECT Branch FROM AEA_Branches");

            HttpResponseMessage wcfResponse = client.GetAsync(url).Result;
            HttpContent stream = wcfResponse.Content;
            var data = stream.ReadAsStringAsync();

            String dataString = data.Result.ToString();
            XmlDocument bodyDoc = new XmlDocument();
            bodyDoc.LoadXml(dataString);

            DataSet ds = new DataSet();

            ds.ReadXml(new XmlTextReader(new StringReader(bodyDoc.InnerXml)), XmlReadMode.ReadSchema);
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now