C#学习笔记之创建带属性的Xml文档

来源:互联网 发布:西门子plc伺服编程 编辑:程序博客网 时间:2024/04/30 01:19
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Xml;namespace 创建带属性的Xml{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            // 声明一个Xml文档对象            XmlDocument doc = new XmlDocument();            // 创建Xml描述信息            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);            doc.AppendChild(dec);            // 创建根节点            XmlElement root = doc.CreateElement("Root");            doc.AppendChild(root);            // 创建子节点            XmlElement books = doc.CreateElement("Books");            root.AppendChild(books);            // 给子节点创建元素            XmlElement book = doc.CreateElement("Book");            book.SetAttribute("name", "西游记"); // 给节点添加属性            book.SetAttribute("price", "33");            books.AppendChild(book);            XmlElement book2 = doc.CreateElement("Book");            book2.SetAttribute("name", "水浒传");            book2.SetAttribute("price", "11");            books.AppendChild(book2);            doc.Save("Xml.xml");            MessageBox.Show("创建成功");        }    }}

0 0
原创粉丝点击