如何让WebServer返回指定XML内容

80酷酷网    80kuku.com

  

  通过创建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";

    }
}

/*引用结束*/



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: