Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert Rest response to DataSet at client c#

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
encoding an href string 2 24
imap mails 1 25
Where did System.Data.Objects go? 2 40
LINQ Select all records by MAX data syntax help 1 17
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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

840 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