c#操作xml文件

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace XMLTest
{
    public class CommXml
    {
        public XmlDocument xmlDoc = new XmlDocument();
        public void getXmlDoc(string strXml)
        {
            xmlDoc.LoadXml(strXml);
        }

        public CommXml(string strXml)
        {
            xmlDoc.LoadXml(strXml);

        }

        /// <summary>
        /// 从父节点开始,中间用逗号隔开
        /// </summary>
        /// <param name="strNode">节点名称</param>
        /// <param name="strValue">节点值</param>
        public void setNodeValue(string strNode, string strValue)
        {
            XmlNode xn = getNode(strNode);
            xn.InnerText = strValue;

        }


        /// <summary>
        /// 根据逗号相隔节点名称取得当前节点
        /// </summary>
        /// <param name="strNode"></param>
        /// <returns></returns>
        private XmlNode getNode(string strNode)
        {
            XmlNode xn = null;

            string[] strNodeNameArr = strNode.Trim(',').Split(',');

            foreach (string strNodeName in strNodeNameArr)
            {
                if (xn == null)
                {
                    if (strNodeName.IndexOf("*") > 0)
                    {
                        xn = getNodeListNode(strNodeName, xmlDoc);
                    }
                    else
                    {
                        xn = xmlDoc.SelectSingleNode(strNodeName);
                    }
                }
                else
                {
                    if (strNodeName.IndexOf("*") > 0)
                    {
                        xn = getNodeListNode(strNodeName, xn);
                    }
                    else
                    {
                        xn = xn.SelectSingleNode(strNodeName);
                    }
                    if (xn == null) throw new Exception("节点" + strNodeName + "不存在.");
                }

            }

            return xn;

        }

        /// <summary>
        /// 节点列表中单个节点取得
        /// </summary>
        /// <param name="strNode">节点列表节点名称:例;nodename*0 指节点名称是nodename的节点数组下标是0的节点取得</param>
        /// <param name="fatherXn">当前节点的父亲节点</param>
        /// <returns></returns>
        private XmlNode getNodeListNode(string strNode, XmlNode fatherXn)
        {
            XmlNode xn = null;
            string[] strNodeListNameAndArrNum = strNode.Split('*');
            XmlNodeList xnList = fatherXn.SelectNodes(strNodeListNameAndArrNum[0]);
            xn = xnList.Item(Convert.ToInt32(strNodeListNameAndArrNum[1]));
            return xn;
        }



        /// <summary>
        ///  节点属性值设定
        /// </summary>
        /// <param name="strNode">从父节点开始,中间用逗号隔开</param>
        /// <param name="strAttributeName">节点名称</param>
        /// <param name="strValue">节点值</param>
        public void setNodeAttributes(string strNode, string strAttributeName, string strValue)
        {
            XmlNode xn = getNode(strNode);
            try
            {
                xn.Attributes[strAttributeName].Value = strValue;
            }
            catch
            {
                throw new Exception("节点" + strNode + "不存在属性[" + strAttributeName + "].");

            }
        }

        /// <summary>
        /// 节点属性值取得
        /// </summary>
        /// <param name="strNode">从父节点开始,中间用逗号隔开</param>
        /// <param name="strAttributeName">节点名称</param>
        /// <returns></returns>
        public string getNodeAributeValue(string strNode, string strAttributeName)
        {
            try
            {
                XmlNode xn = getNode(strNode);
                return xn.Attributes[strAttributeName].Value;
            }
            catch
            {
                throw new Exception("节点" + strNode + "不存在属性[" + strAttributeName + "].");

            }

        }

        /// <summary>
        /// 数组也表数据节点属性值设定
        /// </summary>
        /// <param name="strNode">从父节点开始,中间用逗号隔开</param>
        /// <param name="strAttributeName">节点名称</param>
        /// <param name="strValue">节点值</param>
        /// <param name="arrNum">数组节点下标,从0开始</param>
        public void setNodeListAttributes(string strNode, string strAttributeName, string strValue, int arrNum)
        {

        }


        public void test()
        {

            string strXml = @" <battleArmies>
 <battleArmy side=""1"">
 <battleTroops>
 <battleTroop position=""0"">
 <battleTeams>
 <battleTeam id=""1000"" x=""15"" y=""7"" hp=""80.0"" amount=""8"">
 <soldierConfig name=""老虎"" type=""LAO_HU"" category=""2"" attack=""18.0"" defence=""15.0"" hp=""10.0"" moveSpeed=""10.0"" attackScope=""1.0"" attackSpeed=""15.0"" destroy=""null"" critical=""null"" criticalRate=""null"">
<attackAddon key_0=""2.83"" key_1=""1.0"" key_2=""1.0"" key_3=""1.0"" key_4=""1.0"" />
</soldierConfig>
 <soldierAddonConfig name=""老虎"" type=""LAO_HU"" category=""2"" attack=""18.18"" defence=""15.15"" hp=""10.0"" moveSpeed=""10.0"" attackScope=""1.0"" attackSpeed=""15.0"" destroy=""null"" critical=""null"" criticalRate=""null"">
<attackAddon key_0=""2.83"" key_1=""1.0"" key_2=""1.0"" key_3=""1.0"" key_4=""1.0"" />
</soldierAddonConfig>
</battleTeam>
 </battleTeams>
 </battleTroop>
 </battleTroops>
 </battleArmy>
 </battleArmies>
";
            CommXml xml = new CommXml(strXml);
            xml.setNodeAttributes("battleArmies,battleArmy,battleTroops,battleTroop,battleTeams,battleTeam*0,soldierConfig*0,attackAddon", "key_0", "2999");
            string strNodeValue = xml.getNodeAributeValue("battleArmies,battleArmy,battleTroops,battleTroop,battleTeams,battleTeam*0,soldierConfig*0,attackAddon", "key_0");

        }
    }





}

posted @ 2009-04-23 11:31  ljlxyf  阅读(167)  评论(0编辑  收藏  举报