首页 > 其他 > 详细

创建XML

时间:2016-10-25 02:00:28      阅读:163      评论:0      收藏:0      [点我收藏+]
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace 创建XML
{
    class Program
    {
        static void Main(string[] args)
        {
            //通过代码来创建XML文档
            //1.引用命名空间
            //2.创建XML文档对象 元素包含节点,元素是所有,节点是标签Element>=Node
            
            XmlDocument doc = new XmlDocument();
            //3.第一行的描述信息
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //4.创建根节点 rootNode
            XmlElement books = doc.CreateElement("Books");
            //添加根节点到文件
            doc.AppendChild(books);
            //5.给根节点Boooks创建子节点
            XmlElement book1 = doc.CreateElement("Book");
            //将book添加到根节点
            books.AppendChild(book1);

            //6.给Book1添加子节点
            XmlElement name1 = doc.CreateElement("Name");
            name1.InnerText = "水浒传";
            book1.AppendChild(name1);

            XmlElement price1 = doc.CreateElement("Price");
            price1.InnerText = "100";
            book1.AppendChild(price1);

            XmlElement des1 = doc.CreateElement("Des");
            des1.InnerText = "文言文不好看";
            book1.AppendChild(des1);

            doc.Save("Books.xml");
            Console.WriteLine("保存成功");
            Console.ReadKey();
        }
    }
}

 

创建XML

原文:http://www.cnblogs.com/blacop/p/5995081.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!