我有一个包含学校元素的 XML 文件。
<Classrooms>
<Classroom ID="Mrs.S">
<Students>
<Student>
<Name> Billy Blue </Name>
<Grade> 1 </Grade>
<Sex> Male </Sex>
<Age> 7 </Age>
<Picture> c:/School/Students/BillyBlue </Picture>
</Student>
</Students>
</Classroom>
</Classrooms>
我想在使用 Windows 表单时附加不同的学生。这是我的代码。它们目前被添加到教室标签之后,我希望它们在学生节点中。
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ConfigurationManager.AppSettings.Get("studentFile"));
XmlNode student = xmlDoc.CreateElement("Student");
XmlNode name = xmlDoc.CreateElement("Name");
name.InnerText = tBName.Text;
student.AppendChild(name);
XmlNode grade = xmlDoc.CreateElement("Grade");
grade.InnerText = tBGrade.Text;
student.AppendChild(grade);
XmlNode sex = xmlDoc.CreateElement("Sex");
sex.InnerText = tbSex.Text;
student.AppendChild(sex);
XmlNode age = xmlDoc.CreateElement("Age");
age.InnerText = tBAge.Text;
student.AppendChild(age);
XmlNode picture = xmlDoc.CreateElement("Picture");
picture.InnerText = tBPicture.Text;
student.AppendChild(picture);
xmlDoc.DocumentElement.AppendChild(student);
xmlDoc.Save(ConfigurationManager.AppSettings.Get("studentFile"));
}
慕仙森
喵喔喔
相关分类