?
Solved

Convert Rest response to DataSet at client c#

Posted on 2013-12-01
1
Medium Priority
?
2,640 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
[X]
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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 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