namespace XML { class Program { static void Main(string[] args) { string path = @"C:\Users\Administrator\Desktop\xml\study.xml"; //读取XML文件 XDocument xdoc = XDocument.Load(path); //获取跟节点 XElement root = xdoc.Root; //遍历子节点 foreach (var item1 in root.Elements()) { foreach (var item2 in item1.Elements()) { Console.WriteLine(item2.Name+"========="+item2.Value); } } Console.ReadKey(); } } }
namespace XML { class Program { static void Main(string[] args) { //写入XML文件 XDocument xdoc = new XDocument(); //创建根节点 XElement root = new XElement("Person"); XElement student = new XElement("Student"); student.SetAttributeValue("StuId","1"); student.SetElementValue("name","小丸子"); student.SetElementValue("gender","女"); student.SetElementValue("age","20"); root.Add(student); xdoc.Add(root); //保存 xdoc.Save(@"C:\Users\Administrator\Desktop\xml\1.xml"); Console.WriteLine("创建成功!"); Console.ReadKey(); } } }
时间: 2024-10-24 08:46:17