你的位置:首页 > ASP.net教程

[ASP.net教程]C# 解析XML


解析文件内容:

 

 1 <??> 2  3 <users> 4  <user id="1001"> 5   <name>张三</name> 6   <email>123456789@136.com</email> 7   <phone>13400000000</phone> 8  </user> 9  <user id="1002">10   <name>李四</name>11   <email>123456789@126.com</email>12   <phone>15800000000</phone>13  </user>14 </users>

 

解析代码:

 1 static void ReadParse 2 { 3   new  4   "E:/Data/VisualStudio/C#/app001/ConsoleApp/App01/userlist."); 5   //查找<users> 6   "users"); 7   //获取到所有<users>的子节点 8   root.ChildNodes; 9   //遍历所有子节点10   foreach (in nodeList)11   {12      (13      xe.ChildNodes;14     foreach (in subList)15     {16       if ("name".Equals(17       {18         Console.WriteLine("姓名:" + 19       }20       else if ("email".Equals(21       {22         Console.WriteLine("邮箱:" + 23       }         24     }      25   }26 }27 28 29 30 31 static void ReadParse32 {33   new 34   "E:/Data/VisualStudio/C#/app001/ConsoleApp/App01/userlist.");35   //查找<users>36   "users");37   //获取到所有<users>的子节点38   "users").ChildNodes;39   //遍历所有子节点40   foreach (in nodeList)41   {42      (43     Console.WriteLine("节点的ID为: " + xe.GetAttribute("id"));44      xe.ChildNodes;45     foreach (in subList)46     {47       Console.WriteLine(48     }49   }50 }