我遇到了C#反序列化方法的问题,该方法用于从XML读取数据并将其放入List<>中。我有以下XML文件: events.xml
<ArrayOfEvent xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Event>
<eventID>0</eventID>
<type>Theatre</type>
<title>City of gods</title>
<price>100</price>
<city>London</city>
<place>Culture hall</place>
<date>2014-12-18T20:00:00</date>
<available_tickets>500</available_tickets>
<reserved_tickets>0</reserved_tickets>
</Event>
<Event>
<eventID>1</eventID>
<type>Concert</type>
<title>Jon Hopkins</title>
<price>500</price>
<city>Radbroke</city>
<place>Arena</place>
<date>2014-12-18T20:00:00</date>
<available_tickets>200</available_tickets>
<reserved_tickets>0</reserved_tickets>
</Event>
</ArrayOfEvent>
Event.cs
public class Event
{
public int eventID { get; set; }
public String type { get; set; }
public String title { get; set; }
public double price { get; set; }
public String city { get; set; }
public String place { get; set; }
public DateTime date { get; set; }
public String description { get; set; }
public int available_tickets { get; set; }
public int reserved_tickets { get; set; }
public Event(){}
public Event(int ID, String typ, String titl, double pric, String cit, String plac, DateTime dat, String descriptio, int available_t, int reserved_t ){
eventID = ID;
type = typ;
title = titl;
price = pric;
city = cit;
place = plac;
date = dat;
description = descriptio;
available_tickets = available_t;
reserved_tickets = reserved_t;
}
Database.cs
public List<Event> loadEvents() {
List<Event> events = new List<Event>();
XmlSerializer deserializer = new XmlSerializer(typeof(List<Event>));
TextReader textReader = new StreamReader("events.xml");
events = (List<Event>)deserializer.Deserialize(textReader);
textReader.Close();
return events;
}
EventAdd.cs
Database data = new Database();
public void testEvents(List<Event>){
data.loadEvents();
Console.WriteLine(events.Count.ToString());
}
输出:0
问题是它打印0; 我甚至无法使用该readed列表执行任何操作,因为它是空的。 请帮助我,我这个问题好几天了。
答案 0 :(得分:0)
在您提供课程活动后,如果您有#34;&lt; / ArrayOfEvent&gt;&#34;在你的xml中正确。
我使用以下代码在我的电脑上测试
string xml = "<ArrayOfEvent xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
@"<Event>
<eventID>0</eventID>
<type>Theatre</type>
<title>City of gods</title>
<price>100</price>
<city>London</city>
<place>Culture hall</place>
<date>2014-12-18T20:00:00</date>
<available_tickets>500</available_tickets>
<reserved_tickets>0</reserved_tickets>
</Event>
</ArrayOfEvent>";
List<Event> events = new List<Event>();
XmlSerializer deserializer = new XmlSerializer(typeof(List<Event>));
TextReader textReader = new StringReader(xml);
events = (List<Event>)deserializer.Deserialize(textReader);
textReader.Close();
Console.WriteLine(events.Count);
代码与您的代码类似,但使用StringReader()而不是从文件加载。
the output is 1.
所以问题应该是你的xml文件。 可能在不正确的路径中,可能包含一些额外的字节,如BOM,也许是编码问题。 你需要自己弄明白:)
答案 1 :(得分:0)
根据MSDN样本,
[XmlElement(Namespace = "")]
将包括在每个班级成员之前。尝试将此添加到您的代码中。
来自MSDN的sample就是这个。
public class OrderedItem
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}