XML的学习运用

35 篇文章 0 订阅
2 篇文章 0 订阅

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");
        }

    }
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值