</pre><pre name="code" class="csharp"> protected void init() { string filename = "Student.xml"; DataSet ds = new DataSet(); ds.readxml(filename); dataGridView1.DataSource = ds.Tables[0]; //--------指定查询-------- string name = "王亚晓"; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNode root = doc.SelectSingleNode("student"); XmlNode target = root.ChildNodes[0]; foreach (XmlNode child in root.ChildNodes) { foreach (XmlNode grandson in child.ChildNodes) { if(grandson.Name=="name"&&grandson.InnerText==name) { target = grandson.ParentNode; } } } Console.WriteLine("name:"+target.ChildNodes[0].InnerText); Console.WriteLine("age:"+target.ChildNodes[1].InnerText); Console.WriteLine("sex"+target.ChildNodes[2].InnerText); }
//获取属性 Console.WriteLine(child.Attributes["type"].Name); Console.WriteLine(child.Attributes["type"].Value);