c#遍历xml怎么只显示一条记录
xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<dataset>
<port name="COM2">
<number1>18012345678</number1>
<number2>18023234233</number2>
<number3>18045344534</number3>
<number4>18078645645</number4>
</port>
</dataset>
c#代码:
private void readtreebutton_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("d:\\2.xml");
XmlNodeList xmllist = xmlDoc.SelectSingleNode("dataset/port").ChildNodes;
String[] strlist = new String[xmllist.Count]; // 改动
for (int i = 0; i < xmllist.Count; i++) // 改动
{
strlist[i] = xmllist[i].InnerText;
textBox1.Text = strlist[i].ToString();
}
}
运行后只显示最后一条记录?
------解决方案--------------------for (int i = 0; i < xmllist.Count; i++) // 改动
{
strlist[i] = xmllist[i].InnerText;
textBox1.Text+=strlist[i].ToString()+"\n\r";
}