ʹÓÃXmlDocument¶ÁÈ¡XML½ÚµãËùÓÐÊý¾Ý_XHTML/WEB_ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤»ùµØ--Powered by WWW.RONGSEN.COM.CN

ʹÓÃXmlDocument¶ÁÈ¡XML½ÚµãËùÓÐÊý¾Ý

×÷ÕߣººÚ¿Í·ÀÏßÍø°²XML½Ì³Ì»ùµØ À´Ô´£ººÚ¿Í·ÀÏßÍø°²XML½Ì³Ì»ùµØ ä¯ÀÀ´ÎÊý£º0

±¾Æª¹Ø¼ü´Ê£ºËùÓÐÊý¾Ý½Úµã¶ÁÈ¡
ºÚ¿Í·ÀÏßÍø°²ÍøѶ£º     ÍøÉÏÓкöàASP.NET¶ÁÈ¡XMLµÄÀý×Ó£¬±ÈÈçʹÓÃDatasetÀ´¶ÁÈ¡£¬µ«±¾ÎĽ̳ÌÈ´ÊÇʹÓÃXmlDocumentÀ´¶ÁÈ¡XML½ÚµãÏÂËùÓÐÊý¾Ý£¬ÎÒÃÇÏÈÀ´¿´ÏÂÕâ¸öXML¸ñʽ£ºSysRightsDb.xml XML Code <?xml versio...

     ÍøÉÏÓкöà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>");
    }
}

    ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤·½°¸±¾ÆªÁ¬½Ó£ºhttp://www.rongsen.com.cn/show-16300-1.html
Íøվά»¤½Ì³Ì¸üÐÂʱ¼ä:2012-04-06 01:18:03  ¡¾´òÓ¡´ËÒ³¡¿  ¡¾¹Ø±Õ¡¿
ÎÒÒªÉêÇë±¾Õ¾£ºNµã | ºÚ¿Í·ÀÏß¹ÙÍø |  
רҵ·þÎñÆ÷ά»¤¼°Íøվά»¤ÊÖ¹¤°²È«´î½¨»·¾³£¬ÍøÕ¾°²È«¼Ó¹Ì·þÎñ¡£ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤»ùµØÕÐÉ̽øÐÐÖУ¡QQ:29769479

footer  footer  footer  footer