简体   繁体   中英

Removing xml namespaces in C# restful web service returned string

I am having restful API which is written in C# using dot net framework 4.5..Currently its working alright... i'm returning a result after a JSON conversion.. I'm expecting a pure JSON result... which i'm not getting currently.. I'm expecting simple solution to omit the string XMLNS at the root element where i return the JSON... Result i'am getting: 结果

My code :

public String GetAllSalesInvoices(string customer_id, string Startdate, string Enddate)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        string query = "SELECT * FROM sales_invoice WHERE customer_id =" + customer_id + " AND invoice_date BETWEEN '" + Startdate + "' AND '" + Enddate + "'";
        DataSet ds = conObj.execQuery(query);
        DataTable dt = new DataTable();
        dt = ds.Tables[0];


        List<sales_invoice> result = new List<sales_invoice>();

        foreach (DataRow dr in dt.Rows)
        {
            sales_invoice inv = new sales_invoice()
            {
                Invoice_id = Convert.ToInt32(dr["invoice_id"]),
                Invoice_date = Convert.ToString(dr["invoice_date"].ToString()),
                Customer_id = Convert.ToInt32(dr["customer_id"]),
                Product_id = Convert.ToInt32((dr["product_id"])),
                Time = Convert.ToString((dr["time"]).ToString()),
                Quantity = Convert.ToInt32((dr["quantity"])),
                Unit_of_measure = Convert.ToString(dr["unit_of_measure"]),
                Product_price = Convert.ToInt32((dr["product_price"])),
                Sub_total = Convert.ToInt32((dr["sub_total"])),
            };
            result.Add(inv);
        }
        string json=serializer.Serialize(result);
        return json;
}

Thanks

Hi guys i finally managed to come up with an answer for removing the xml namespaces or string namespaces in return. Solution is to use the context method and write content directly to browser it self. For that you can use this code.

String result =js.Serialize(data);
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(result);

This will write json serialized raw data in to the browser without any namespaces. Note that it cannot be used in context of restful api as the this might be different there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM