C#之XML基础 创建只有第一行描述信息

来源:互联网 发布:求一个矩阵的逆过程 编辑:程序博客网 时间:2024/06/07 13:12
镇场诗:慈心积善融学习,技术誓为大家学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。

——————————————————————————————————————————————————————————


1、代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;namespace ConsoleApplication3{    class Program    {        static void Main(string[] args)        {            XmlDocument xml = new XmlDocument();            //创建第一行描述信息            //                                                       版本   编码格式             XmlDeclaration theFirstRowOfXml=  xml.CreateXmlDeclaration("1.0","utf-8",null);            xml.AppendChild(theFirstRowOfXml);            //保存之前要创建根节点,根节点的名字是Books            XmlElement Books= xml.CreateElement("Books");            //再把创建的根节点加入到xml中            xml.AppendChild(Books);            xml.Save("只有第一行描述信息.xml");            Console.WriteLine("OK");            Console.ReadKey();        }    }}



2、控制台效果



3、XML效果



4、不添加根节点的异常


0 0
原创粉丝点击