1 <?xml version="1.0" encoding="utf-8"?> 2 <root> 3 <person name="WangYao"> 4 <age>25</age> 5 </person> 6 <person name="Jobs"> 7 <age>56</age> 8 </person> 9 <person name="LILY"> 10 <age>15</age> 11 </person> 12 <person name="LUCY"> 13 <age>35</age> 14 </person> 15 </root>
namespace button2读XML { public partial class Form1 : Form { XmlDocument doc = new XmlDocument(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { doc.Load(@"C:\Users\ASUS\Desktop\XMLFile3.xml "); //加载Xml文件 XmlElement rootElem = doc.DocumentElement; //获取根节点 XmlNodeList personNodes = rootElem.GetElementsByTagName("person"); //获取person子节点集合 foreach (XmlNode node in personNodes) { string strName = ((XmlElement)node).GetAttribute("name"); //获取name属性值 Console.WriteLine(strName); XmlNodeList subAgeNodes = ((XmlElement)node).GetElementsByTagName("age"); //获取age子XmlElement集合 // Console.WriteLine(subAgeNodes); if (subAgeNodes.Count == 1) { string strAge = subAgeNodes[0].InnerText; textBox1.Text = strAge; } } } } }
XML 和代码如上,现在 textBox1.Text只可以显示最后的值,也就是35,但如果我想显示任意一个值,我怎么做呢?
都取出来,下拉列表对应textBox显示,想要那个点那个
什么意思??你是想根据person 的name找到对应的age??