簡體   English   中英

如何在ASP.net Web服務中返回XML中修改標記名稱?

[英]How to modify Tag names in return XML in ASP.net Web service?

跟隨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>

根標記和子Tage是NewDataSet和Table。 是否可以更改此標簽名稱?

我需要如下進行更改。

NewDataSet =員工

表=員工

從您的xml中獲取一個字符串並進行文本替換:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM