2015-10-13 55 views
0

以下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 =員工

表=僱員

回答

0

路線從您的XML字符串,做一個文本替換:

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

這不是working.Nothing發生 – Keey