C#简单创建xml文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ConsoleDemo
{
    class Programme
    {
        public static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", "no");
            doc.AppendChild(dec);

            XmlElement root=doc.CreateElement("info");
            doc.AppendChild(root);

            XmlElement stu = doc.CreateElement("student");
            stu.SetAttribute("id", "001");
            root.AppendChild(stu);

            XmlElement name = doc.CreateElement("name");
            name.InnerText = "小明";
            stu.AppendChild(name);

            doc.Save("info.xml");
        }
    }
}

 创建的xml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<info>
  <student id="001">
    <name>小明</name>
  </student>
</info>
 

你可能感兴趣的:(xml)