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; 7 8 namespace Day08_XML 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 XmlDocument doc = new XmlDocument(); 15 doc.Load("Computer.xml"); 16 XmlNode root=doc.DocumentElement; 17 foreach(XmlNode item in root.ChildNodes){ 18 string id = item.Attributes["id"].Value; 19 20 21 Console.WriteLine(id); 22 Console.WriteLine("名称为"+item["Name"].InnerText); 23 24 25 } 26 27 //foreach(XmlNode item in root.ChildNodes){ 28 29 //item是XML中的Computer对象 30 // foreach(XmlNode child in item.ChildNodes){ 31 32 // switch(child.Name){ 33 34 // case "Name": 35 // Console.WriteLine("我的电脑是:"+child.InnerText); 36 // break; 37 38 39 40 // } 41 42 43 // } 44 45 46 // } 47 Console.ReadLine(); 48 } 49 } 50 }
1 <?xml version="1.0" encoding="utf-8" ?> 2 <Computer> 3 <Computers id="110"> 4 <Name>联想</Name> 5 </Computers> 6 <Computers id="120"> 7 <Name>苹果</Name> 8 </Computers> 9 </Computer>