如何在ASP.net Web服务中修改返回XML中的标签名称?

问题描述:

以下Web服务执行存储过程并返回XML时没有任何错误。如何在ASP.net 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> 

根标记和子标记是NewDataSet和Table。有没有可能改变这个标签名称?

我需要改变它如下。

NewDataSet =员工

表=雇员

路线从您的XML字符串,做一个文本替换:

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

这不是working.Nothing发生 – Keey