【问题标题】:How to modify Tag names in return XML in ASP.net Web service?如何在 ASP.net Web 服务中修改返回 XML 中的标记名称?
【发布时间】:2016-01-10 20:24:51
【问题描述】:

以下 web 服务执行存储过程并返回 XML 没有任何错误。

using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Xml;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]


// 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 GetData : System.Web.Services.WebService
{

    public GetData()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public XmlElement GetUserDetailsXML(string userName)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
        con.Open();
        // SqlCommand cmd = new SqlCommand("select * from tblUserInformation where UserName like @userName+'%'", con);
        SqlCommand cmd = new SqlCommand("sp_GetEmployeeData", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@USERID", userName);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        // Create an instance of DataSet.
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();

        // Return the DataSet as an XmlElement.
        XmlDataDocument xmldata = new XmlDataDocument(ds);
        XmlElement xmlElement = xmldata.DocumentElement;
        return xmlElement;

    }

}

输出如下。

<NewDataSet>

    <Table>
        <TimeStamp>2015-10-12T14:53:02.15+05:30</TimeStamp>
        <Name>Albert</Name>
        <Value>1</Value>
    </Table>

    <Table>
        <TimeStamp>2015-10-12T15:17:15.143+05:30</TimeStamp>
        <Name>Albert</Name>
        <Value>12</Value>
    </Table>

</NewDataSet>

Root Tag 和 Child Tag 是 NewDataSet 和 Table。是否可以更改此标签名称?

我需要如下更改。

NewDataSet = 员工

表 = 员工

【问题讨论】:

    标签: c# asp.net xml web-services iis


    【解决方案1】:

    从您的 xml 中获取一个字符串并进行文本替换:

    xmlElement.OuterXml.Replace("NewDataSet ", "Employees")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多