Tom-Net

导航

xmlparser.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace cdqy.kup
{
    /// <summary>
    /// XML解析
    /// </summary>
    public class Parser
    {
        /// <summary>
        /// 读取XML字段
        /// </summary>
        /// <param name="strTree"></param>
        /// <param name="rank"></param>
        /// <param name="strAttribute"></param>
        /// <returns></returns>
        public static XmlNode readNode(string strSection, string strRank)
        {
            string strConfig = Utils.ReadFile(Utils.MapPath("/app.xml"));
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(strConfig);
            XmlNodeList xnl = xmlDoc.GetElementsByTagName(strSection);
            if (!String.IsNullOrEmpty(strRank))
            {
                return xnl[Convert.ToInt32(strRank)];
            }
            else
            {
                return xnl[0];
            }
        }

        public static XmlNode readNode(XmlNode node, string strSeciton, string strRank)
        {
            if (node != null)
            {
                foreach (XmlNode s in node.ChildNodes)
                {
                    if (s.Name == strSeciton)
                    {
                        return s;
                    }
                }
                return null;
            }
            else
            {
                return readNode(strSeciton, strRank);
            }
        }

        public static XmlNode getNode(string[] strSection, string[] strRank)
        {
            XmlNode tempXn = null;
            for (int i = 0; i < strSection.Length; i++)
            {
                if (i == 0)
                {
                    tempXn = readNode(null, strSection[i], strRank[i]);
                }
                else
                {
                    tempXn = readNode(tempXn, strSection[i], strRank[i]);
                }
            }
            return tempXn;
        }

        public static XmlNode getNode(string[] strSection)
        {
            XmlNode tempXn = null;
            for (int i = 0; i < strSection.Length; i++)
            {
                if (i == 0)
                {
                    tempXn = readNode(null, strSection[i], "0");
                }
                else
                {
                    tempXn = readNode(tempXn, strSection[i], "0");
                }
            }
            return tempXn;
        }
    }
}

posted on 2008-10-12 22:56  Tom_Net  阅读(114)  评论(0)    收藏  举报