控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

C# xml文件读取

作者:佚名 出处:互联网 2011年03月22日 阅读:

C# 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();
        }

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统