ÍøÉÏÓкöàASP.NET¶ÁÈ¡XMLµÄÀý×Ó£¬±ÈÈçʹÓÃDatasetÀ´¶ÁÈ¡£¬µ«±¾ÎĽ̳ÌÈ´ÊÇʹÓÃXmlDocumentÀ´¶ÁÈ¡XML½ÚµãÏÂËùÓÐÊý¾Ý£¬ÎÒÃÇÏÈÀ´¿´ÏÂÕâ¸öXML¸ñʽ£ºSysRightsDb.xml
XML Code
<?xml version="1.0" encoding="utf-8" ?>
<root>
<rights name="SYS">
<xml name="¹ÉƱÐÐÇé" code="stockmarket"></xml>
<xml name="ÒµÎñϵͳ" code="sales"></xml>
<xml name="¿Í·þϵͳ" code="servcice"></xml>
<xml name="²ÆÎñϵͳ" code="financial"></xml>
<xml name="ºô½ÐÖÐÐÄϵͳ" code="callcenter"></xml>
<xml name="±¨±íϵͳ" code="report"></xml>
</rights>
<rights name="financial">
<xml name="¶©µ¥¹ÜÀí" code="so_mana"></xml>
<xml name="¶©µ¥Áбí" code="so_list"></xml>
<xml name="ÉêÇëÉó¼Æ" code="so_audit_list"></xml>
<xml name="ȨÏÞÁбí" code="so_ur_sour_list"></xml>
</rights>
</root>
´ÓÕâ¸öXML¿ÉÒÔ¿´Ï£¬ÎÒ½«Ê¹ÓÃASP.NET¶ÁȡȨÏÞϵͳÖнڵãÊôÐÔµÈÓÚSYSÏÂËùÓÐÊý¾Ý£¬¼°¶ÁÈ¡financial½ÚµãÏÂËùÓÐÊý¾Ý¡£
ÕâÖÖXMLÊý¾Ý¸ñʽÈÃÈË¿´ÆðÀ´ºÜÃ÷È·¾ÍÖªµÀËù´ú±íµÄÒâ˼£¬¿´ÆðÀ´Ò²²»´í¡£
ÏÂÃ濪ʼ°É¡£
µÚ1¡¢Ê×ÏÈ´´½¨¶ÁÈ¡XMLÀàxmlHepler£¬ÄÚÈÝÈçÏ£º
ÏÈÒýÓÃÁ½¸öÃüÃû¿Õ¼ä
using System.Xml;
using System.Collections;
xmlHepler Code
/// <summary>
/// ×÷Õߣºdodo
/// ÍøÕ¾£ºwww.xueit.com
///
/// ¶ÁÈ¡XMLÀà
/// </summary>
/// <typeparam name="T"></typeparam>
public class xmlHepler<T>:System.Web.UI.Page
{
Hashtable table = new Hashtable();
T FileName;
T Root; //¸ù½Úµã
T RootAttName; //½ÚµãÊôÐÔÃû³Æ
T RootAttValue; //¸ù½ÚµãÊôÐÔÖµ
T Field; //Xml×Ö¶Î
/// <summary>
/// XMLÎļþ·¾¶
/// </summary>
/// <param name="val"></param>
public xmlHepler(T val)
{
FileName = val;
this.LoadXml(val.ToString());
}
/// <summary>
/// XMLÎļþ·¾¶
/// </summary>
/// <param name="file"></param>
private void LoadXml(string file)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(file);
table.Add("xml", xdoc);
}
/// <summary>
/// ·µ»ØXML to DataTable
/// </summary>
/// <returns></returns>
public DataTable GetXmlToDataTable()
{
string[] SplitField=Field.ToString().Split(',');
//¹¹ÔìDataTable
DataTable dt = new DataTable();
DataColumn dc = null;
for (int i = 0; i < SplitField.Length; i )
{
dc = new DataColumn(SplitField[i]);
dt.Columns.Add(dc);
}
XmlDocument xdoc = (XmlDocument)table["xml"];
XmlNodeList xTable = xdoc.DocumentElement.SelectNodes(Root.ToString());
foreach (XmlNode xnode in xTable)
{
if (xnode.Attributes[RootAttName.ToString()].InnerText == RootAttValue.ToString()) //ijһ½Úµã
{
//¸Ã½ÚµãÏÂËùÓÐ×Ó½Úµã
XmlNodeList xnlist = xnode.ChildNodes;
//×Ó½ÚµãËùÓÐÊý¾Ý
for (int i = 0; i < xnlist.Count; i ) //for (int i = 0; i < xnode.ChildNodes.Count; i ) Õâ¾äÊÇËùÓÐxml×Ó½ÚµãÊý¾Ý
{
DataRow dr = dt.NewRow();
//°ó¶¨ËùÐè×Ö¶Î
for (int j = 0; j < SplitField.Length; j )
{
dr[SplitField[j]] = xnode.ChildNodes[i].Attributes[SplitField[j]].Value;
}
dt.Rows.Add(dr);
}
}
}
return dt;
}
#region ÉèÖÃÖµ
/// <summary>
/// ¸ù½Úµã
/// </summary>
public T xmlRoot
{
get { return Root; }
set { Root = value; }
}
/// <summary>
/// ½ÚµãÊôÐÔ×Ö¶ÎÃû³Æ
/// </summary>
public T xmlRootAttName
{
get { return RootAttName; }
set { RootAttName = value; }
}
/// <summary>
/// ½ÚµãÊôÐÔ×Ö¶ÎÖµ
/// </summary>
public T xmlRootAttValue
{
get { return RootAttValue; }
set { RootAttValue = value; }
}
/// <summary>
/// ×Ó½ÚµãÊôÐÔ×Ö¶Î
/// </summary>
public T xmlSplitField
{
set { Field = value; }
}
#endregion
}
Õâ¸öxmlHeplerÀàʹÓÃDocumentElement.SelectNodesÀ´Ñ¡ÔñXML½Úµã£¬Ö®ºóif (xnode.Attributes[RootAttName.ToString()].InnerText == RootAttValue.ToString())»ñÈ¡Ïà¹ØÊôÐԵĽڵã.
ʹÓ÷½·¨GetXmlToDataTable()À´¹¹ÔìDataTable£¬°ÑXML½ÚµãÏÂËùÓÐÊý¾Ýµ¼Èëµ½DataTable¡£
Õâ¸öÀàµÄÔ´Âë¹Ø¼üµÄµØ·½¶¼Ð´ÁË×¢ÊÍ£¬ºÜ¼òµ¥¡£
ÏÂÃæÎÒÃÇÀ´¿´¿´ÈçºÎµ÷ÓôËÀࣺ
ÔÚ³ÌÐòPage_Load²âÊÔµ÷ÓÃSysRightsDb.xml£¬²¢°ÑÏà¹ØÊý¾ÝÏÔʾ³öÀ´¡£
Ô´ÂëÈçÏ£º
µ÷ÓÃxmlHepler Code
protected void Page_Load(object sender, EventArgs e)
{
xmlHepler<string> xml = new xmlHepler<string>(Server.MapPath("SysRightsDb.xml"));
xml.xmlRoot = "rights";
// µ÷ÓÃSYS½ÚµãÏÂÊý¾Ý
xml.xmlRootAttName = "name";
xml.xmlRootAttValue = "SYS";
xml.xmlSplitField = "code,name";
DataTable dt = xml.GetXmlToDataTable();
Response.Write("<b>ϵͳȨÏÞ</b><br>");
foreach (DataRow dr in dt.Rows)
{
Response.Write("name:" dr["name"].ToString() " code:" dr["code"].ToString());
Response.Write("<br>");
}
// µ÷ÓÃfinancial½ÚµãÏÂÊý¾Ý
xml.xmlRootAttName = "name";
xml.xmlRootAttValue = "financial";
xml.xmlSplitField = "code,name";
dt = xml.GetXmlToDataTable();
Utils.Response("<b>×ÓϵͳȨÏÞ</b><br>");
foreach (DataRow dr in dt.Rows)
{
Response.Write("name:" dr["name"].ToString() " code:" dr["code"].ToString());
Response.Write("<br>");
}
}