1 xml文件内容
<?xml version="1.0" encoding="utf-8" ?> <cultures> <daojia> <book> <name> 道德经</name> <author>老子</author> </book> <book> <name> 文始真经</name> <author>关尹子</author> </book> </daojia> <fojia> <book> <name>金刚经</name> <author>释迦牟尼佛</author> </book> <book> <name>地藏菩萨本愿经</name> <author>地藏王菩萨</author> </book> </fojia> </cultures>
2 代码
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 //1.xml文件的格式必须是正确的 15 XDocument file = XDocument.Load("1.xml"); 16 //获取xml文件的根节点 17 XElement xle = file.Root; 18 19 20 IEnumerable<XElement> ixt=xle.Elements(); 21 22 foreach (var item in ixt) 23 { 24 Console.WriteLine(item.Name); 25 } 26 27 Console.ReadKey(); 28 } 29 } 30 }
3 效果
时间: 2024-10-15 22:34:42