C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using System.Xml;
using System.Drawing;
namespace ConsoleApplication1
{
classProgram
{
staticvoid CreateUserList(string path)
{
//创建XmlDocument对象xmlDoc
XmlDocument xmlDoc = newXmlDocument();
//创建一个XML文档声明,并添加到文档
XmlDeclaration declare = xmlDoc.CreateXmlDeclaration("1.0", "utf-8",
"yes");
xmlDoc.AppendChild(declare);
//创建并添加UserList结点
XmlElement userListEle = xmlDoc.CreateElement("UserList");
xmlDoc.AppendChild(userListEle);
//创建并添加Count属性
XmlAttribute countAttr = xmlDoc.CreateAttribute("Count");
countAttr.Value = "1";
userListEle.Attributes.Append(countAttr);
//创建并添加User结点
XmlElement userEle = xmlDoc.CreateElement("User");
userListEle.AppendChild(userEle);
//创建并添加ID属性
XmlAttribute idAttr = xmlDoc.CreateAttribute("ID");
idAttr.Value = "001";
userEle.Attributes.Append(idAttr);
//创建并添加Name元素
XmlElement nameEle = xmlDoc.CreateElement("Name");
nameEle.InnerText = "李四";
userEle.AppendChild(nameEle);
//通过Save()方法保存数据到XML文件UserList.XML中
xmlDoc.Save(path);
}
staticvoid Main(string[] args)
{
string path = "W:\\Users\\Long\\Desktop\\success.xml";
CreateUserList(path);
}
}
}
生成的xml文件