嘿,我有以下代码将列表中的数据存储到 XML 文件中,但是当我将第二个项目添加到列表中时,它只会覆盖 XML 中的第一个项目,因此 XML 文件中只有一个项目,如何我解决这个
班级
public class Visits
{
/*
* This class represents a single appointment
*/
private string Customer_Name;
private string Customer_Address;
private DateTime Arrival_Time;
private string Visit_Type;
private Double Lat1;
private Double Long1;
//Private methods. Note the use of DateTime to store arrival time
public string name{
//Description property
set { Customer_Name = value; }
get {return Customer_Name;}
}
public string address
{//Time property
set { Customer_Address = value; }
get { return Customer_Address; }
}
public DateTime arrival
{ //Duration property
set { Arrival_Time = value; }
get { return Arrival_Time; }
}
public string type
{
set { Visit_Type = value; }
get { return Visit_Type; }
}
public Double Lat
{
//Description property
set { Lat1 = value; }
get { return Lat1; }
}
public Double Lon1
{
//Description property
set { Long1 = value; }
get { return Long1; }
}
public override string ToString()
{ //Return a String representing the object
return Visit_Type + " " + Customer_Name + " " + Customer_Address + " " + Arrival_Time.ToString() + " " + "Latitude " + Lat1 + " " + "Longitude " + Long1;
}
}
}
然后列表
class List
{
/*
* This object represents the List. It has a 1:M relationship with the Visit class
*/
private List<Visits> visits = new List<Visits>();
//List object use to implement the relationshio with Visits
public void addVisits(Visits vis)
{
//Add a new Visit to the List
visits.Add(vis);
}
public List<String> listVisits()
{//Generate a list of String objects, each one of which represents a Visit in List.
List<String> listVisits = new List<string>();
//This list object will be populated with Strings representing the Visits in the lists
foreach (Visits vis in visits)
{
String visAsString = vis.ToString();
//Get a string representing the current visit object
listVisits.Add(visAsString);
//Add the visit object to the List
}
return listVisits;
//Return the list of strings
}
public Visits getVisits(int index)
{
//Return the visit object at the <index> place in the list
int count = 0;
foreach (Visits vis in visits)
{
//Go through all the visit objects
if (index == count)
//If we're at the correct point in the list...
return vis;
//exit this method and return the current visit
count++;
//Keep counting
}
return null;
//Return null if an index was entered that could not be found
}
}
}
然后添加代码
thePickup.name = txtCustName.Text;
thePickup.address = txtCustAddress.Text;
thePickup.arrival = DateTime.Parse(txtArrival.Text);
thePickup.Dname = txtDeliveryName.Text;
thePickup.Daddress = txtDaddress.Text;
thePickup.Lat = Double.Parse(txtLat.Text);
thePickup.Lon1 = Double.Parse(txtLong.Text);
thePickup.type = "Pickup";
//Update thePickup object to reflect any changes made by the user
XmlSerializer SerializerObj = new XmlSerializer(typeof(Pickups));
using (TextWriter WriteFileStream = new StreamWriter(@"Pickup.xml", true))
{
SerializerObj.Serialize(WriteFileStream, thePickup);
}
当我添加一个新条目时它只是改变了原始条目的格式