Solved

Convert Rest response to DataSet at client c#

Posted on 2013-12-01
1
2,249 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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