我的问题是,我的程序没有从我的 XML 文件中获取数据并将其放入列表中
我可以很好地保存数据,但我无法将其加载回来。
这是我的保存功能
public void Save_Data_Click(object sender, EventArgs e)
{
XmlSerializer PSR = new XmlSerializer(typeof(Pickup));
XmlSerializer DSR = new XmlSerializer(typeof(Delivery));
TextWriter PickupStream = new StreamWriter(@"Pickup Save Data.xml");
TextWriter DeliveryStream = new StreamWriter(@"Delivery Save Data.xml");
PSR.Serialize(PickupStream, thePickup);
DSR.Serialize(DeliveryStream, theDelivery);
DeliveryStream.Close();
PickupStream.Close();
MessageBox.Show("All data saved!");
}
这是我的 xml 加载代码
XmlSerializer SerializerObj = new XmlSerializer(typeof(Pickup));
FileStream ReadFileStream = new FileStream(@"Pickup Save Data.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Pickup LoadedObj = (Pickup)SerializerObj.Deserialize(ReadFileStream);
ReadFileStream.Close();
这是我的接送班
namespace Vans
{
[Serializable]
public class Pickup
{
public List<Pickups> Pickups = new List<Pickups>();
public void addPickup(Pickups Pic)
{
Pickups.Add(Pic);
}
public List<String> listPickups()
{
List<String> listPickups = new List<string>();
foreach (Pickups pick in Pickups)
{
String pickupString = pick.ToString();
listPickups.Add(pickupString);
}
return listPickups;
}
public Pickups getPickup(int i)
{
int c = 0;
foreach (Pickups Pic in Pickups)
{
if (i == c)
return Pic;
c++;
}
return null;
}
}
}
我的 Delivery 类与 Pickup 类相同。
编辑
<?xml version="1.0" encoding="utf-8"?>
<Pickup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Pickups>
<Pickups>
<pickupfirstname>dsf</pickupfirstname>
<pickuplastname>sdf</pickuplastname>
<pickupdeliveryaddress>sd</pickupdeliveryaddress>
<pickuptime>dsf</pickuptime>
</Pickups>
</Pickups>
</Pickup>
这是一些示例 XML 代码