如何让WebServer返回指定XML内容_Xml教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐: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教程/更新时间:2008-08-22
相关Xml教程