国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > XML > 正文

如何讓WebServer返回指定XML內容

2024-09-05 20:54:09
字體:
來源:轉載
供稿:網友

  通過創建WEBServer代理可以當作本地類使用,但能不能返回指定的XML呢?比如通過checkpass服務檢測帳號和密碼之后要返回該用戶擁有的權限列表。怎么實現呢?


  ASP.NET Web服務支持在公共語言運行時中支持的所有基本數據類型,包括String,integer,Long等等。除了簡單的基本數據類型之外,還支持基本數據類型的數組。


  但是,更有趣的是支持用戶定義的類和結構體。基本上,任何可由XSD模式代表的類型都是可以作為ASP.NET的參數或返回類型。


  通過一個星期的摸索,解決了這個問題,并學習了如何讀取和輸出XML文檔;數據庫操作;WebServer的創建和引用。下面就部分源碼供初學習者參考,不足之此請指正。


/*CheckLogin服務*/

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using mysql.SQL;
using myfunc.Common;


/// <summary>
/// CheckLogin 的摘要說明
/// </summary>
[WebService(Namespace = "http://localhost/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CheckLogin : System.Web.Services.WebService {

public CheckLogin () {

//如果使用設計的組件,請取消注釋以下行
//InitializeComponent();
}

//[WebMethod(Description = "Login", EnableSession = true)]
[WebMethod]
public checkuser Login(string sUserCode, string sPassword)
{
checkuser objcheckuser= new checkuser();
string sCheckLogin = ConfigurationManager.AppSettings["strCheckLogin"];
SqlShell objShell = new SqlShell();
SqlCommand objCommand = new SqlCommand(sCheckLogin);
objCommand.CommandType = CommandType.Text;
objCommand.Parameters.AddWithValue("@sUserCode", sUserCode);
objCommand.Parameters.AddWithValue("@sPassword", sPassword);
DataTable objDataTable = objShell.executeDataSet(ref objCommand).Tables[0];
objcheckuser.logined = (objDataTable.Rows.Count > 0);
if (objcheckuser.logined)
{
//帳號和密碼正確,反回帳號信息
DataRow objDataRow = objDataTable.Rows[0];
objcheckuser.userid = objDataRow["UserID"].ToString().Trim(); ;
objcheckuser.pass = objDataRow["Pass"].ToString().Trim();
objcheckuser.username = objDataRow["UserName"].ToString().Trim();

//檢查Allow字段是否為空
if (objDataRow.IsNull("Allow")) { objcheckuser.allow = ""; }
else { objcheckuser.allow = objDataRow["Allow"].ToString().Trim(); }

menulist objmenulist = new menulist(objDataRow["UserID"].ToString().Trim());
objcheckuser.menuxml = objmenulist.buf;//返回菜單列表的XML字符串

}
return objcheckuser;

}

public class checkuser
{
public bool logined;
public string userid;
public string pass;
public string username;
public string allow;
public string menuxml;//返回菜單列表的XML字符串

}

}

/*CheckLogin服務結束*/

/*menulist 類開始*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using Singcn.SQL;
using System.Data.SqlClient;
using System.IO;
using System.Text;

namespace myfunc.Common
{
/// <summary>
/// PubFunc 的摘要說明
/// </summary>
public class menulist
{
public XmlWriterSettings settings = new XmlWriterSettings();
public XmlWriter writer = null;
public string buf = "";
public SqlShell objShell;
public SqlCommand objCommand;
public DataTable objDataTable;

public menulist(string userid)
{
objShell = new SqlShell();
objCommand = new SqlCommand("select * from qxdmb order by jb,px,qxdm");
objCommand.CommandType = CommandType.Text;
objDataTable = objShell.executeDataSet(ref objCommand).Tables[0];

StringWriter writerstr = new StringWriter();
settings.Indent = true;
settings.Encoding = Encoding.GetEncoding("utf-8");
try
{
writer = XmlWriter.Create(writerstr, settings);

writer.WriteStartDocument();
writer.WriteStartElement("DSTreeRoot");
writer.WriteAttributeString("text", "后臺管理系統-[" userid "]");
writer.WriteAttributeString("treeId", "0000");
writer.WriteAttributeString("open", "true");


readqxdmb("0");

writer.WriteEndElement();
writer.WriteEndDocument();
}
finally
{
if (writer != null)
writer.Close();
}
buf = writerstr.ToString();
buf = buf.Replace(@"encoding=""utf-16""", @"encoding=""utf-8""");//在使用StringWriter 作為xml輸出時XML自動為“utf-16”,此處用Replace方法處理,如有更好的方法請指教!

}

private void readqxdmb(string sjdm)//生成XML樹的方法
{
DataTable mytable = objDataTable.Copy();
DataRow[] foundRows;
foundRows = mytable.Select("sjdm='" sjdm "'");
if (foundRows.Length > 0)
{
//寫子節點
for (int i = 0; i < foundRows.Length; i )
{
writer.WriteStartElement("DSTree");
writer.WriteAttributeString("text", foundRows[i]["qxsm"].ToString().Trim());
writer.WriteAttributeString("treeId", foundRows[i]["qxdm"].ToString().Trim());
writer.WriteAttributeString("open", "false");
//處理下級節點
readqxdmb((string)foundRows[i]["qxdm"]);
writer.WriteEndElement();
}
}
mytable.Dispose();
}

}
}


/*menulist 結束*/

/*引用開始 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
CheckLogin objCheckLogin=new CheckLogin();
CheckLogin.checkuser objcheckuser = new CheckLogin.checkuser();
objcheckuser=objCheckLogin.Login(TextBox1.Text, TextBox2.Text);
if (objcheckuser.logined) Label1.Text = objcheckuser.userid;
else Label1.Text = "false";

}
}


/*引用結束*/

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莒南县| 龙门县| 怀宁县| 富平县| 嘉兴市| 民勤县| 南昌县| 永宁县| 若尔盖县| 尚志市| 平江县| 班戈县| 临猗县| 建德市| 大石桥市| 海盐县| 诸城市| 肥西县| 富蕴县| 牟定县| 南召县| 宁津县| 嘉禾县| 大石桥市| 云南省| 游戏| 长子县| 道孚县| 永嘉县| 莱西市| 临夏市| 长岭县| 彭水| 淮北市| 台北县| 荣成市| 顺昌县| 康定县| 张掖市| 宜宾县| 合水县|