简体   繁体   English

在C#restful web service中删除xml名称空间返回的字符串

[英]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: 我正在使用dot net framework 4.5编写的用C#编写的restful API。当前它的工作正常...我在JSON转换后返回结果..我期待一个纯粹的JSON结果......我是我目前没有...我期待简单的解决方案在根元素处省略字符串XMLNS,我返回JSON ...结果我得到: 结果

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. 大家好,我终于设法提出了一个回答,删除xml名称空间或字符串名称空间作为回报。 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. 这会将json序列化的原始数据写入浏览器而不包含任何名称空间。 Note that it cannot be used in context of restful api as the this might be different there. 请注意,它不能在restful api的上下文中使用,因为这可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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