我是C#的新手,我想问一下如何将读者(XML文件)的值输入表列。我希望读者的价值是一个字符串。我试图将值发送到listbox1并且它可以工作,但这不是我需要的。谢谢。
private void button1_Click(object sender, EventArgs e)
{
string file;
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) ;
file= ofd.FileName;
XmlTextReader reader = new XmlTextReader(file);
XmlNodeType type;
DataSet dsload = new DataSet();
dsload.ReadXml(file);
while (reader.Read())
{
if (reader.Name == "name")
{
reader.Read();
listBox1.Items.Add(reader.Value);
DataTable dt = new DataTable("MyTable");
dt.Columns.Add(new DataColumn("Name", typeof(string)));
data.DataSource = dsload.Tables[0];
}
}
答案 0 :(得分:0)
while (reader.Read())
{
if (reader.Name == "name")
{
reader.Read();
listBox1.Items.Add(reader.Value);
}
}
DataTable dt = new DataTable("MyTable");
dt.Columns.Add(new DataColumn("Name", typeof(string)));
foreach (string value in listbox1.Items)
{
dr = dt.NewRow();
dr[0] = value;
dt.Rows.Add(dr);
}
data.DataSource=dt;