1. 程式人生 > >C#修改XML節點的值

C#修改XML節點的值

XmlDocument MyXml = new XmlDocument();
            MyXml.Load(@"Config.xml");

            //獲取<Rule>節點的所有子節點
            XmlNodeList MyNodeList = MyXml.SelectSingleNode("Rule").ChildNodes;


            //遍歷<Rule>下的所有子節點
            foreach (XmlNode MyNode in MyNodeList)
            {
                if (MyNode.Name == "InfoPage")
                {
                    XmlNodeList InfoPageNodeList = MyNode.ChildNodes;

                    //遍歷InfoPage節點
                    foreach (XmlNode InfoPageNode in InfoPageNodeList)
                    {
                        if (InfoPageNode.Name == "BookClass")
                        {
                            InfoPageNode.InnerText = "玄幻魔法";
                            break;
                        }
                    }
                    break;
                   
                }

                MyXml.Save(@"Config.xml"); //儲存