11
Answers

Calling Web Service Method in c#

Photo of Sharad Gupta

Sharad Gupta

9y
2.7k
1
Hi 
 
I am calling a web service method  void type
 
[WebMethod(enableSession: true)]
public void BRANCHES(String desc)
{
DataTableToJSON(ds.Tables[0]);
return; 
 
public void DataTableToJSON(DataTable table)
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow row in table.Rows)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
dict[col.ColumnName] = row[col];
}
list.Add(dict);
}
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
String abcd = serializer.Serialize(list).Replace("[", "").Replace("]", "");
HttpContext.Current.Response.BinaryWrite(encoding.GetBytes(abcd));
 
}
but  when i calling this we service method in my page it gives a error "Response is not well-formed XML"
 
how can i solve it .
 
 
basically this method firstly  serializer  
 
"System.Web.Script.Serialization.JavaScriptSerializer serializer"  in format.
 
 
 

Answers (11)

0
Photo of Hemlata Kushwaha
NA 417 34.2k 9y
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
String abcd = serializer.Serialize(list).Replace("[", "").Replace("]", "");
HttpContext.Current.Response.BinaryWrite(encoding.GetBytes(abcd));
 
Replace your code against my code.
System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(jSearializer.Serialize(list));
 
0
Photo of Hemlata Kushwaha
NA 417 34.2k 9y
Ok, But i have developed a webservice which is return json format and called using above code. so , it's working fine. But without seeing your webservice, i can't suggest. what do you want?
0
Photo of Sharad Gupta
NA 19.7k 8.7m 9y
 
i check it online  it return correct format
0
Photo of Hemlata Kushwaha
NA 417 34.2k 9y
I think your webservice not return json in correct format.
0
Photo of Sharad Gupta
NA 19.7k 8.7m 9y
 
No It is not wroking
 
error in
 
  var objects = JArray.Parse(json);
 
"Unexpected character encountered while parsing value: <. Line 3, position 1. "
0
Photo of Hemlata Kushwaha
NA 417 34.2k 9y
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
getarticlelist();
}
private void getarticlelist()
{
StringBuilder str = new StringBuilder();
string url = "Put your Service url";
WebRequest request = WebRequest.Create(url);
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string json = reader.ReadToEnd();
var serializer = new JavaScriptSerializer();
var objects = JArray.Parse(json);
str.Append("<table>");
foreach (JObject root in objects)
{
string id = "";
string title = "";
string date = "";
string longdesc = "";
foreach (KeyValuePair<String, JToken> app in root)
{
if (app.Key == "id") id = app.Value.ToString();
if (app.Key == "title") title = app.Value.ToString();
if (app.Key == "date") date = app.Value.ToString();
if (app.Key == "longdesc") longdesc = app.Value.ToString();
}
str.Append("<tr><td><h1>" + title + "</h1></td></tr>");
str.Append("<tr><td>" + date + "</td></tr>");
str.Append("<tr><td>" + longdesc + "</td></tr>");
}
str.Append("</table>");
//---------Social Sharing
divShare.InnerHtml = str.ToString();
}
}
}
}
//-------aspx  code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="divShare" runat="server">
</div>
</form>
</body>
</html>
//-------------
 
I think this your solution for calling WebService with c# .
Check this Code. 
0
Photo of Sharad Gupta
NA 19.7k 8.7m 9y
 
i have only service reference i cant't change web service method.\
 
i only want to use that method in my c# code and use json string in my  code
0
Photo of Sharad Gupta
NA 19.7k 8.7m 9y
basically this method return json string but i am not able to use  that method in my c# page 
 
because this method gives a error "Response is not well-formed XML" .
 
 
simply i want to get this method json string in my c# page. after it i will convert back into datatable 
0
Photo of Hemlata Kushwaha
NA 417 34.2k 9y
check this code.
0
Photo of Hemlata Kushwaha
NA 417 34.2k 9y
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void GetDataTable(string id)
    {
        List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
        IndianAstrology IA = new IndianAstrology();
        DataTable dt = IA.GetDataTable("select * from tablename");
        foreach (DataRow row in dt.Rows)
        {
            Dictionary<string, object> dict = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {
                dict[col.ColumnName] = row[col];
            }
            list.Add(dict);
        }
        System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        this.Context.Response.ContentType = "application/json; charset=utf-8";
        this.Context.Response.Write(jSearializer.Serialize(list));
    }
0
Photo of Saineshwar Bageri
79 23.5k 24.1m 9y
You want out put in Json or XML