C# 写XML文件

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

namespace 写xml
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "C:\\Users\\CMS01\\Desktop\\tool.xml";//文件路径
            XElement rootEle = new XElement("Project",
                                            new XAttribute("Version", "1.0"));//根节点
            XElement propertyEle = new XElement("PropertyGroup");//子节点
            for(int i = 0;i<10;i++)
            {
                propertyEle.Add(new XElement("OutputName", "测试的字符"+i));
            }

            rootEle.Add(propertyEle);//将子节点加入到根节点
            var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), rootEle);//给xml流加个头
            xDoc.Save(filename);//将xDoc保存为filename这个路径的文档
        }
    }
}

xml文件效果:

 

posted @ 2022-08-02 09:17  羽小兮  阅读(199)  评论(0)    收藏  举报