【发布时间】:2016-05-11 14:46:29
【问题描述】:
我正在编写一个将其数据存储到 XML 的程序。我被困在从 XML 获取数据到 DataGridView 和编辑 XML 元素值上。我尝试了很多代码,但它不起作用。我究竟做错了什么? 哪个更容易。 Linq 命令或 XMLWriter、XMLReader 方式???? 代码如下:
private void button2_Click(object sender, EventArgs e)
{
string sid = textBox1.Text;
string fname = textBox3.Text;
string lname = textBox2.Text;
string address = textBox4.Text;
string gender = radioButton1.Checked ? "эр" : "эм";
XmlDocument doc = new XmlDocument();
doc.Load("student.xml");
string tempid = doc.SelectSingleNode("root/Student/ID").InnerText;
if(tempid == sid)
{
doc.SelectSingleNode("root/Student/Fname").InnerText = fname;
doc.SelectSingleNode("root/Student/Lname").InnerText = lname;
doc.SelectSingleNode("root/Student/Address").InnerText = address;
doc.SelectSingleNode("root/Student/Gender").InnerText = gender;
}
还有 XML:
<?xml version="1.0" encoding="UTF-8"?>
-<root>
-<Student>
<ID>B140030123</ID>
<FName>Kent</FName>
<LName>Wayne</LName>
<Address>Gotham</Address>
<Gender>эр</Gender>
</Student>
</root>
如何循环请帮助:(
private void button2_Click(object sender, EventArgs e)
{
string sid = textBox1.Text;
string fname = textBox3.Text;
string lname = textBox2.Text;
string address = textBox4.Text;
string gender = radioButton1.Checked ? "эр" : "эм";
XmlDocument doc = new XmlDocument();
doc.Load("student.xml");
XmlNode node = doc.DocumentElement;
string tempid = doc.SelectSingleNode("root/Student/ID").InnerText;
foreach (var temp in doc.SelectSingleNode("root/Student/ID"))
{
if (tempid == sid)
{
doc.SelectSingleNode("root/Student/FName").InnerText = fname;
doc.SelectSingleNode("root/Student/LName").InnerText = lname;
doc.SelectSingleNode("root/Student/Address").InnerText = address;
doc.SelectSingleNode("root/Student/Gender").InnerText = gender;
}
}
doc.Save("student.xml");
}
【问题讨论】:
-
我假设有一个 doc.Save("student.xml") 命令?
标签: c# xml linq datagridview