c#怎么样写入下面的数据到listview 控件,显标表格中
c#怎么样写入下面的数据到listview 控件,显标表格中<?xmlversion="1.0"encoding="utf-8"?><Data...
c#怎么样写入下面的数据到listview 控件,显标表格中<?xml version="1.0" encoding="utf-8"?><Data> <Account> <List id="0441" MINGZI="天使" chengji="90" Zone="合格" JIEGUO="通过" /> <List id="630" MINGZI="小明1" chengji="80" Zone="合格" JIEGUO="通过" /> <List id="13903" MINGZI="天梦" chengji="50" Zone="合格" JIEGUO="通过" /> <List id="3132" MINGZI="小明星在" chengji="88" Zone="合格" JIEGUO="通过" /> <List id="3193" MINGZI="小生在线" chengji="99" Zone="合格" JIEGUO="通过" /> </Account></Data>
展开
1个回答
展开全部
用 XmlDocument、XmlNodeList、XmlElement 是一种方法,这也是笔者经常使用的方法
public Form1()
{
InitializeComponent();
LoadXml();
}
private void LoadXml()
{
listView1.View = View.Details;
if (listView1.Width < 300) listView1.Width = 300;
if (Width < listView1.Left + listView1.Width) Width = listView1.Left * 2 + 15 + listView1.Width;
listView1.Columns.Add("序号");
listView1.Columns.Add("名字");
listView1.Columns.Add("成绩");
listView1.Columns.Add("评价");
listView1.Columns.Add("结果");
string file = Application.StartupPath + "\\" + "XMLFile1.xml";
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load(file);//xd.LoadXml()用于加载字符串的xml
System.Xml.XmlNodeList xnl = xd.SelectNodes("/Data/Account/List");
System.Xml.XmlElement xe = null;
for (int i = 0; i < xnl.Count; i++)
{
xe = (System.Xml.XmlElement)xnl[i];
ListViewItem lvi = new ListViewItem(xe.GetAttribute("id"));
lvi.SubItems.Add(xe.GetAttribute("MINGZI"));
lvi.SubItems.Add(xe.GetAttribute("chengji"));
lvi.SubItems.Add(xe.GetAttribute("Zone"));
lvi.SubItems.Add(xe.GetAttribute("JIEGUO"));
listView1.Items.Add(lvi);
}
}
希望回答对你有帮助
public Form1()
{
InitializeComponent();
LoadXml();
}
private void LoadXml()
{
listView1.View = View.Details;
if (listView1.Width < 300) listView1.Width = 300;
if (Width < listView1.Left + listView1.Width) Width = listView1.Left * 2 + 15 + listView1.Width;
listView1.Columns.Add("序号");
listView1.Columns.Add("名字");
listView1.Columns.Add("成绩");
listView1.Columns.Add("评价");
listView1.Columns.Add("结果");
string file = Application.StartupPath + "\\" + "XMLFile1.xml";
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load(file);//xd.LoadXml()用于加载字符串的xml
System.Xml.XmlNodeList xnl = xd.SelectNodes("/Data/Account/List");
System.Xml.XmlElement xe = null;
for (int i = 0; i < xnl.Count; i++)
{
xe = (System.Xml.XmlElement)xnl[i];
ListViewItem lvi = new ListViewItem(xe.GetAttribute("id"));
lvi.SubItems.Add(xe.GetAttribute("MINGZI"));
lvi.SubItems.Add(xe.GetAttribute("chengji"));
lvi.SubItems.Add(xe.GetAttribute("Zone"));
lvi.SubItems.Add(xe.GetAttribute("JIEGUO"));
listView1.Items.Add(lvi);
}
}
希望回答对你有帮助
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询