C# xml文件读取

前两篇学习了配置文件的读取,接下来学习xml文件的读取:

假设有xml文件:ParamConfig.xml

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <books kinds="7">
    <book name="math" price="20"></book>
    <book name="Chinese" price="10"></book>
    <book name="English" price="15"></book>
    <book name="chemistry" price="25"></book>
    <book name="biology" price="20"></book>
    <book name="physics" price="30"></book>
    <book name="history" price="15"></book>
  </books>
</configuration>

       //文件路径

       string filePath = System.AppDomain.CurrentDomain.BaseDirectory + @"\ParamConfig.xml";

        /// <summary>
        /// 加载xml
        /// </summary>
        private void LoadXML()
        {
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory + @"\ParamConfig.xml";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);
            XmlNode parent, root;
            parent = xmlDoc.SelectSingleNode("configuration");
            if (null == parent)
            {
                return;
            }
            root = parent.SelectSingleNode("books");
            if (null == root)
            {
                return;
            }
            XmlNodeList nodeList = root.ChildNodes;
            if (null == nodeList || nodeList.Count <= 0)
            {
                return;
            }
            foreach (XmlNode node in nodeList)
            {
                XmlElement element = node as XmlElement;
                this.richTextBox1.AppendText(string.Format("name={0},price={1}", element.GetAttribute("name"),

                                                            element.GetAttribute("price")) + "\r\n");
            }
        }

        /// <summary>
        /// 保存xml
        /// </summary>
        private void SaveXML()
        {
            List<book> bookList = new List<book>();
            bookList.Add(new book("math", "25"));
            bookList.Add(new book("Chinese", "5"));
            bookList.Add(new book("Biology", "10"));
            XmlDocument xmlDoc = new XmlDocument();
            if (!System.IO.File.Exists(filePath))
            {
                XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
                xmlDoc.AppendChild(dec);
            }
            else
            {
                xmlDoc.Load(filePath);
            }
            XmlNode parent, root;
            parent = xmlDoc.SelectSingleNode("configuration");
            if (null == parent)
            {
                parent = xmlDoc.CreateElement("configuration");
                xmlDoc.AppendChild(parent);
            }
            root = parent.SelectSingleNode("books");
            if (null == root)
            {
                root = xmlDoc.CreateElement("books");
                parent.AppendChild(root);
            }
            root.RemoveAll();
            ((XmlElement)root).SetAttribute("kinds", bookList.Count.ToString());
            foreach (book pbook in bookList)
            {
                XmlElement bookElement = xmlDoc.CreateElement("book");
                bookElement.SetAttribute("name", pbook.name);
                bookElement.SetAttribute("price", pbook.price);
                root.AppendChild(bookElement);
            }
            xmlDoc.Save(filePath);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveXML();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            LoadXML();
        }

posted @ 2011-03-20 02:38  freeman_rain  阅读(657)  评论(0编辑  收藏  举报