You must Sign In to post a response.
  • Category: WCF

    Wcf error There was an error deserializing the object of type System.String. End element 'root' from

    try
    {
    WebClient proxy = new WebClient();
    byte[] data = proxy.DownloadData("http://localhost:36/Service.svc/GetData");
    Stream stream = new MemoryStream(data);
    DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));
    string result = obj.ReadObject(stream).ToString();
    Console.WriteLine(result.ToString());
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    error There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'd' from namespace '
    There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'd' from namespace '
  • #769018
    Hi,

    This issue might come because of couple of reasons.
    If you are not declared the variables properly you might get this issue and one more thing is if array was not used in a proper manner.


    [DataMember]
    private string _name;
    public string player_name


    The above will give this same error you got. To overcome we should declare like below.

    private string _name;
    [DataMember]
    public string player_name


    If the variable is not mandatory make it as false.

    [DataMember(Name = "RequestId", IsRequired = false)]


    Also check your Json format. Most of the issue are happen because of Json format.

    Thanks,
    Mani

  • #769019
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service : IService
    {


    public string GetData()
    {


    SqlConnection con = new SqlConnection("Data Source=.;Persist Security Info=True;password=CACHEmemory1234;User ID=sa;Initial Catalog=HRAdminAffairs");
    con.Open();
    SqlCommand cmd = new SqlCommand("getNationality", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@CompanyName", "hasoub");


    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    cmd.ExecuteNonQuery();
    con.Close();
    DataTable dt = ds.Tables[0];
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    Dictionary<string, object> row;
    foreach (DataRow dr in dt.Rows)
    {
    row = new Dictionary<string, object>();
    foreach (DataColumn col in dt.Columns)
    {
    row.Add(col.ColumnName, dr[col]);
    }
    rows.Add(row);
    }
    return serializer.Serialize(rows);
    }
    [ServiceContract]
    public interface IService
    {
    //[WebGet(UriTemplate = "/Employees", ResponseFormat = WebMessageFormat.Xml)]
    // [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    [WebInvoke( Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

    // [WebGet(UriTemplate = "GetData", ResponseFormat = WebMessageFormat.Json)]
    string GetData();

    //[DataContract]
    //public class employee
    //{
    // [datamember]
    // public datatable employeetable
    // {
    // get;
    // set;
    // }
    //}
    }


Sign In to post your comments