我有xml文档包含数据:
<patients>
<patient>
<firstname>Patience_name_1</firstname>
<lastname>Patience_surname_1</lastname>
<age>20</age>
<gender>Male</gender>
<exams>
<exam>
<examname>1th Exam of Patient 1</examname>
<id>1</id>
<date>2/29/2016 12:18:44</date>
<comment>**1th Exam completed for patience1</comment>
</exam>
</exams>
</patient>
<patient>
<firstname>Patience_name_2</firstname>
<lastname>Patience_surname_2</lastname>...
我读了文件并更新了exp。 firstname并用新文件名编写(&#34; editedList.xml&#34;)。
public List<patient> ReadXML()
{
List<patient> overview = new List<patient>();
try
{
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(patients));
System.IO.StreamReader file = new System.IO.StreamReader("data.xml");
var data = (patients)reader.Deserialize(file);
file.Close();
overview = data.patients_list.ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
}
return overview;
}
public void WriteXML(string patientName)
{
List <patient> patients = this.ReadXML();
patients.Where(w => w.name == patientName).Select(s =>
{
s.name = DataKeeper.selectedPatient.name.ToString();
s.surname = DataKeeper.selectedPatient.surname.ToString();
s.gender= DataKeeper.selectedPatient.gender.ToString();
return s;
} ).ToList();
try
{
patients p = new patients();
p.patients_list = patients;
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(typeof(patients));
var wfile = new System.IO.StreamWriter(@"editedList.xml");
writer.Serialize(wfile, patients);//Throw exception
wfile.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
}
}
在阅读中没有问题,在我编辑一个字段后我想写它但上面的行显示了一个异常&#34;生成XML文档时出错。&#34;信息。
EDITED 堆栈跟踪:
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
at WpfApplication1.DataReader.WriteXML(String patientName) in C:\Users\yener.yilmaz\Documents\Visual Studio 2015\Projects\WpfApplication1\WpfApplication1\DataReader.cs:line 49