using System;
using System.Windows.Forms;
using System.Xml;
namespace winformDemo
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//XmlDocument打开文档
XmlDocument xc = new XmlDocument();
/// <summary>
/// 显示所有商品的名字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
// xc.Load(写入要获取文档的路径)
xc.Load("D:\\Text\\shopping.xml");
//XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;获取shopping下面的子节点放到集合里。
XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;
//循环去取子节点
foreach (XmlNode xn in xnl)
{
//获取商品名字。
//this.textBox1.Text += xn.Attributes["id"].Value + "\r\n";
foreach (XmlNode xnd in xn.ChildNodes)
{
if (xnd.Name == "Shopname")
{
textBox1.Text += "商品名字:" + xnd.InnerText + "\r\n";
}
}