Based on the index in the controller, how would I append a record to an XML file? I've done some research, but I can't seem to wrap my head around it.
Index(controller)
public ActionResult Index(string sortOrder)
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Users\\Matt.Dodson\\Desktop\\SampleWork\\PersonsApplicationFromXMLFile\\PersonsApplicationFromXMLFile\\DAL\\Personal.xml");
IEnumerable<Personal> persons = doc.SelectNodes("/Persons/record").Cast<XmlNode>().Select(node => new Personal()
{
ID = node["ID"].InnerText,
Name = node["Name"].InnerText,
Email = node["Email"].InnerText,
DateOfBirth = node["DateOfBirth"].InnerText,
Gender = node["Gender"].InnerText,
City = node["City"].InnerText
});
switch (sortOrder)
{
case "ID":
persons = persons.OrderBy(Personal => Personal.ID);
break;
case "Name":
persons = persons.OrderBy(Personal => Personal.Name);
break;
case "City":
persons = persons.OrderBy(Personal => Personal.City);
break;
default:
break;
}
return View(persons.ToList());
}
What I've tried:
Create(Controller)
[HttpPost]
public ActionResult Create(FormCollection collection)
{
string xmlFile = "C:\\Users\\Matt.Dodson\\Desktop\\SampleWork\\PersonsApplicationFromXMLFile\\PersonsApplicationFromXMLFile\\DAL\\Personal.xml";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
IEnumerable<Personal> persons = doc.SelectNodes("/Persons/record")
.Cast<XmlNode>()
.Select(node => new Personal()
{
ID = node["ID"].InnerText,
Name = node["Name"].InnerText,
Email = node["Email"].InnerText,
DateOfBirth = node["DateOfBirth"].InnerText,
Gender = node["Gender"].InnerText,
City = node["City"].InnerText
});
persons.appendTo(xmlFile);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
I am bad at syntax so this is probably all wrong.