我的问题是,我的程序没有从我的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;
}
}
}
My 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代码
答案 0 :(得分:0)
在this示例中,应该(解)序列化的每个字段都有一个注释[XmlElement]
,也许这就是原因......
答案 1 :(得分:0)
确保您的Pickups
课程如下所示反序列化:
public class Pickups
{
public string pickupfirstname;
public string pickuplastname;
public string pickupdeliveryaddress;
public string pickuptime;
}
注意:我已经在本地测试了您的代码并包含了这个类,并且它运行得非常好。