C# 操作Xml之创建XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace XML_Practice
{
class Program
{
static void Main(string[] args)
{
XmlDocument xDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xDoc.AppendChild(xmlDeclaration);
XmlElement rootElement = xDoc.CreateElement("School");
xDoc.AppendChild(rootElement);
XmlElement classElement = xDoc.CreateElement("class");
XmlAttribute classId= xDoc.CreateAttribute("id");
classId.Value = "cl01";
classElement.Attributes.Append(classId);
rootElement.AppendChild(classElement);
XmlElement studentElement = xDoc.CreateElement("Student");
XmlAttribute studentId = xDoc.CreateAttribute("Id");
studentId.Value = "st01";
studentElement.Attributes.Append(studentId);
classElement.AppendChild(studentElement);
xDoc.Save("standard.xml");
Console.WriteLine("写入成功!");
Console.ReadKey();
}
}
}
学习参考视频