如何创建xml或json数据并将其作为Web服务返回到C#中?

时间:2022-10-24 12:50:54

I am creating a C# ASP.NET Web Service application. I need to access data from a SQL database and return this data as XML or JSON. When you run the HelloWorld method (see the code below), it returns the data in XML. But I need to make my own XML tags from the SQL data. How is this done?

我正在创建一个C#ASP.NET Web服务应用程序。我需要从SQL数据库访问数据并将此数据作为XML或JSON返回。当您运行HelloWorld方法(请参阅下面的代码)时,它将以XML格式返回数据。但我需要从SQL数据中创建自己的XML标记。这是怎么做到的?

In an addition, how can I make the web service use JSON, not XML?

另外,如何使Web服务使用JSON而不是XML?

As an example, say I'm returning a table set of 2 rows and 2 columns from SQL and I format it in XML:

举个例子,假设我从SQL返回一个包含2行和2列的表集,并用XML格式化它:

<returnSet>
  <row1>
    <FirstName>
      David
    </FirstName>
    <LastName>
      Faustino
    </LastName>
  </row1>
  <row2>
    <FirstName>
      Henry
    </FirstName>
    <LastName>
      Irons
    </LastName>
  </row2>
</returnSet>

Thank you very much for any assistance.

非常感谢您的帮助。

UPDATE

UPDATE

I modiifed the code below for the inclusion of returning JSON. The problem is when I debug the application and click on the method, it still shows XML:

我修改了下面的代码,以包含返回的JSON。问题是当我调试应用程序并单击该方法时,它仍然显示XML:

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">"Hello World"</string>

My expectations have been write the code below, then goto Debug => Start Debugging. Click HelloWorld and, voila, it returns data in the form of JSON. I see elsewhere that some are calling it using jQuery or plain JavaScript. Is that the only way to test that it's returning JSON data?

我的期望是编写下面的代码,然后转到Debug => Start Debugging。单击HelloWorld,瞧,它以JSON的形式返回数据。我在其他地方看到有些人正在使用jQuery或纯JavaScript来调用它。这是测试它返回JSON数据的唯一方法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace WebApplication2
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {

            JavaScriptSerializer js = new JavaScriptSerializer();

            return js.Serialize("Hello World");
        }
    }
}

1 个解决方案

#1


1  

You can just add another attribute below [WebMethod] on your HelloWorld method to get json:

您可以在HelloWorld方法的[WebMethod]下面添加另一个属性来获取json:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

#1


1  

You can just add another attribute below [WebMethod] on your HelloWorld method to get json:

您可以在HelloWorld方法的[WebMethod]下面添加另一个属性来获取json:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]