【小白专用】C#创建XML文件 更新2024.1.1

 public static void CreateXml(string strPath)
 {


     string directPath = Path.Combine(strPath, "AppData");
     string strFileName = directPath + @"/EmployeesList.xml";

     //创建xml文档
     XmlDocument xmldoc = new XmlDocument();

     //加入XML声明
     XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
     xmldoc.AppendChild(xmldecl);

     // 创建一个注释节点  
     XmlNode comment = xmldoc.CreateComment("这是一个注释");
     xmldoc.AppendChild(comment);

     //加入一个根元素
     XmlElement employees = xmldoc.CreateElement("Employees");
     xmldoc.AppendChild(employees);

     //根元素加入一些子元素
     for (int i = 1; i < 5; i++)
     {
         // 创建一个注释节点  
         XmlNode comment1 = xmldoc.CreateComment("Employee" + i);
         employees.AppendChild(comment1);

         XmlElement employee = xmldoc.CreateElement("Employee" + i);//创建一个子节点
         employee.SetAttribute("name", "人员" + i);//设置该节点属性

         XmlElement age = xmldoc.CreateElement("Age");//创建一个孙节点
         age.InnerText = (18 + i).ToString();//设置文本
         employee.AppendChild(age);//添加到子节点中

         XmlElement sex = xmldoc.CreateElement("Sex");//创建一个孙节点
         sex.InnerText = i % 2 == 0 ? "男" : "女";//设置文本
         employee.AppendChild(sex);//添加到子节点中

         XmlElement salary = xmldoc.CreateElement("Salary");
         salary.InnerText = (10000 + i * 100).ToString();
         employee.AppendChild(salary);

         employees.AppendChild(employee);//将子节点添加到根节点中 
     }
    

     //保存创建好的XML文档
     xmldoc.Save(strFileName);




 }
<?xml version="1.0" encoding="UTF-8"?>
<!--这是一个注释-->
<Employees>
  <!--Employee1-->
  <Employee1 name="人员1">
    <Age>19</Age>
    <Sex>女</Sex>
    <Salary>10100</Salary>
  </Employee1>
  <!--Employee2-->
  <Employee2 name="人员2">
    <Age>20</Age>
    <Sex>男</Sex>
    <Salary>10200</Salary>
  </Employee2>
  <!--Employee3-->
  <Employee3 name="人员3">
    <Age>21</Age>
    <Sex>女</Sex>
    <Salary>10300</Salary>
  </Employee3>
  <!--Employee4-->
  <Employee4 name="人员4">
    <Age>22</Age>
    <Sex>男</Sex>
    <Salary>10400</Salary>
  </Employee4>
</Employees>

public static void CreateXml(string strPath)
{

    string directPath = Path.Combine(strPath, "AppData");
    string strFileName = directPath +@"/PensonList.xml";

    List<Penson> list = new List<Penson>();

    list.Add(new Penson { name = "罗分明", age = 18, gender = "男" });
    list.Add(new Penson { name = "lqwvje", age = 19, gender = "男" });
    list.Add(new Penson { name = "LFM", age = 20, gender = "男" });


    //创建一个XmlDocument对象
    XmlDocument xmlDoc = new XmlDocument();
    //添加xml文件头的声明
    XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

    //将创建的xml文件声明追加到XmlDocument对象里
    xmlDoc.AppendChild(dec);

    //创建一个标签名为list的节点
    XmlElement list1 = xmlDoc.CreateElement("list");
    xmlDoc.AppendChild(list1);

    for (int i = 0; i < list.Count; i++)
    {
        XmlElement xmlName = xmlDoc.CreateElement("name");
        xmlName.SetAttribute("id", i.ToString());//添加属性
        xmlName.InnerText = list[i].name;
        list1.AppendChild(xmlName);

        XmlElement xmlAge = xmlDoc.CreateElement("age");
        xmlAge.InnerText = (list[i].age).ToString();
        list1.AppendChild(xmlAge);

        XmlElement xmlGender = xmlDoc.CreateElement("gender");
        xmlGender.InnerText = list[i].gender;
        list1.AppendChild(xmlGender);
    }


    xmlDoc.Save(strFileName);
    MessageBox.Show("ok");





}

<?xml version="1.0" encoding="utf-8"?>
<list>
  <name id="0">罗分明</name>
  <age>18</age>
  <gender>男</gender>
  <name id="1">lqwvje</name>
  <age>19</age>
  <gender>男</gender>
  <name id="2">LFM</name>
  <age>20</age>
  <gender>男</gender>
</list>