Мне нужно десериализовать XML-файл в List или Array, затем увеличить этот List или Array на 1, а затем снова сериализовать файл, добавив новый объект в этот XML-файл.
Я написал что-то подобное, но это не работает:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void deserialize()
{
string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
using (FileStream fs = new FileStream(path, FileMode.Open))
{
XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
List<Person> persons = (List<Person>)ser.Deserialize(fs);
fs.Close();
//List<Person> persons1 = ((List<Person>)os.Length + 1);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
if (File.Exists(path))
{
deserialize();
}
else
{
List<Person> o = new List<Person>(TextBox1.Text, TextBox2.Text, int.Parse(TextBox3.Text));
using (FileStream fs = new FileStream(path, FileMode.Create))
{
XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
ser.Serialize(fs, o);
}
}
}
}
спасибо за любую помощь:)