XML的学习运用

来源:互联网 发布:mac ps合并图层快捷键 编辑:程序博客网 时间:2024/05/19 14:38

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Xml;using System.Windows.Forms;namespace XmlApplication{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            xmlConfig();            LoadXML();        }        /// <summary>        /// 生成XML        /// </summary>        private void xmlConfig()        {            XmlDocument doc = new XmlDocument();            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "gb2312", null);            doc.AppendChild(dec);            XmlElement root = doc.CreateElement("唐诗");            doc.AppendChild(root);            XmlNode node = doc.CreateElement("五言绝句");            root.AppendChild(node);            XmlNode node1 = doc.CreateElement("作者");            System.Xml.XmlAttribute ab = doc.CreateAttribute("字号");            ab.Value = "太白";            node1.InnerText = "李白";            node1.Attributes.Append(ab);            node.AppendChild(node1);            XmlNode node2 = doc.CreateElement("标题");            node2.InnerText = "静夜思";            node.AppendChild(node2);            XmlNode node3 = doc.CreateElement("内容");            node3.InnerText = "床前明月光,疑是地上霜。举头望明月,低头思故乡。";            node.AppendChild(node3);            doc.Save(Application.StartupPath+@"\tangshi.xml");        }        /// <summary>        /// 获取指定节点的属性和值        /// </summary>        private void LoadXML()        {            XmlDocument doc = new XmlDocument();            doc.Load(Application.StartupPath + @"\tangshi.xml");            XmlElement element = doc.DocumentElement;            XmlNodeList list = doc.GetElementsByTagName("作者");            for (int i = 0; i < list.Count; i++)            {                textBox2.Text = list[i].InnerText;                if (list[i].Attributes[i].Name == "字号")                {                    textBox5.Text = list[i].Attributes[i].Value;                }            }            XmlNode node = doc.SelectSingleNode("唐诗/五言绝句/作者");            textBox2.Text = node.InnerText;            for (int j = 0; j < node.Attributes.Count; j++)            {                if (node.Attributes[j].Name == "字号")                {                    textBox5.Text = list[j].Attributes[j].Value;                }            }            //using (XmlReader reader = XmlReader.Create(Application.StartupPath + @"\tangshi.xml"))            //{            //    while (reader.Read())            //    {            //        switch (reader.NodeType)            //        {            //            case XmlNodeType.Element:            //                    string value=reader.Value;            //                break;            //            case XmlNodeType.Text:            //                break;            //            default: break;            //        }            //    }            //}            using (XmlTextReader reader = new XmlTextReader(Application.StartupPath + @"\tangshi.xml"))            {                while (reader.Read())                {                    if (reader.NodeType == XmlNodeType.Text)                    {                        string s = reader.Value;                    }                }            }        }        /// <summary>        /// 修改指定节点里面的属性和值        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button1_Click(object sender, EventArgs e)        {            XmlDocument doc = new XmlDocument();            doc.Load(Application.StartupPath + @"\tangshi.xml");            XmlNodeList list = doc.GetElementsByTagName("作者");            for (int i = 0; i < list.Count; i++)            {                list[i].InnerText = "杜甫";                textBox2.Text = list[i].InnerText;                if (list[i].Attributes[i].Name == "字号")                {                    list[i].Attributes[i].Value = "小白";                    textBox5.Text = list[i].Attributes[i].Value;                }            }            doc.Save(Application.StartupPath + @"\tangshi.xml");        }        /// <summary>        /// 删除指定节点        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button2_Click(object sender, EventArgs e)        {            XmlDocument doc = new XmlDocument();            doc.Load(Application.StartupPath + @"\tangshi.xml");            //删除“五言绝句”节点里面的“作者”节点            XmlNode node = doc.SelectSingleNode("唐诗/五言绝句/作者");            doc.SelectSingleNode("唐诗/五言绝句").RemoveChild(node);            doc.Save(Application.StartupPath + @"\tangshi.xml");        }        /// <summary>        /// 替换指定节点        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button3_Click(object sender, EventArgs e)        {            XmlDocument doc = new XmlDocument();            doc.Load(Application.StartupPath + @"\tangshi.xml");            //删除“五言绝句”节点里面的“作者”节点            XmlNode node = doc.SelectSingleNode("唐诗/五言绝句/作者");            XmlNode newNode=doc.CreateElement("律师");            XmlAttribute atu = doc.CreateAttribute("乳名");            atu.Value = "小律师";            newNode.Attributes.Append(atu);            newNode.InnerText = "利克斯";            doc.SelectSingleNode("唐诗/五言绝句").ReplaceChild(newNode,node);            doc.Save(Application.StartupPath + @"\tangshi.xml");        }        /// <summary>        /// 在指定节点内增加新的节点        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button4_Click(object sender, EventArgs e)        {            //在“唐诗”节点里增加宋词子节点            XmlDocument doc = new XmlDocument();            doc.Load(Application.StartupPath + @"\tangshi.xml");            XmlNode newNode= doc.CreateElement("宋词");            XmlAttribute atu = doc.CreateAttribute("代表人");            atu.Value = "李清照";            newNode.Attributes.Append(atu);            newNode.InnerText = "女人";            doc.SelectSingleNode("唐诗").AppendChild(newNode);            doc.Save(Application.StartupPath + @"\tangshi.xml");        }    }}


 

原创粉丝点击