如何讓WebServer返回指定XML內容_Xml教程
推薦:XML編程實例:用ASP XML打造留言本一、讀者指引 讀者指引幫助你掌握本文的梗概。以免你看了大半才明白這編文章不適合你,給你造成視覺污染。 如果你正在用ASP XML寫一些程序,或者你正在學XML那就值得一看。 閱讀本文須具
通過創建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";
}
}
/*引用結束*/
分享:一個Asp與XML交互的實例源碼XML 是標準擴展語言,是未來Web編程的標準,asp 是現在廣為流傳的web編程語言之一,能不能讓他們兩個聯合起來發揮作用呢?豆腐在這里給大家提供一個很簡單的例子關于XML和XSL限于篇幅和知識水平豆
- 相關鏈接:
- 教程說明:
Xml教程-如何讓WebServer返回指定XML內容。