c# xml文件新增同级节点_C# XML 给xml文件添加根节点

1 代码

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Text;

5 using System.Threading.Tasks;

6 using System.Xml.Linq;

7

8 namespace ConsoleApplication8

9 {

10 class Program

11 {

12 static void Main(string[] args)

13 {

14 XDocument xmlFile = new XDocument();

15

16 //设置根元素

17 XElement root =new XElement("cultures");

18

19 //添加根节点并且只能一个

20 xmlFile.Add(root);

21 //保存文件

22 xmlFile.Save("test.xml");

23

24 //读取程序写好的xml文件,打印到控制台

25 var readXml= XDocument.Load("test.xml");

26 Console.WriteLine(readXml.Root);

27

28 Console.ReadKey();

29 }

30 }

31 }

2 效果

3 其实xml还有一行文本没有被打印出来

时间: 10-15

你可能感兴趣的:(c#,xml文件新增同级节点)