我使用此代碼收到異常;
InnerException: System.InvalidOperationException
以下是我認爲的相關代碼。
Message=The specified type was not recognized: name='Person',
namespace='', at "<"Contact xmlns=''>.
Person類是沒有任何註解的裸類,並且不從任何接口繼承。
如何使反序列化識別我的類? 在此先感謝。使用XmlSerializer進行反序列化時出錯
public class Contacts : List<Contact.Contact>
{
private void PopulateTypeList()
{
types.Add(typeof(Contact.Contact));
types.Add(typeof(Contact.Company));
types.Add(typeof(Contact.Person));
types.Add(typeof(ContactData.Direction));
types.Add(typeof(ContactData.email));
types.Add(typeof(ContactData.Phone));
}
public void Load()
{
try
{
using (System.Xml.XmlReader stream = System.Xml.XmlReader.Create(fileName))
{
XmlSerializer xs = new XmlSerializer(typeof(List<Contact.Contact>));
// this roundabout way is for making it possible for this class to
// inherit from List<Contact.Contact> and still use a method that
// gives the stored data as an value in the object
here is error List<Contact.Contact> data =
(List<Contact.Contact>)xs.Deserialize(stream);
this.Clear();
this.AddRange(data);
}
}
catch (System.IO.FileNotFoundException)
{
// do nothing; no file, new database
}
}
public void Save()
{
using (System.Xml.XmlWriter stream = System.Xml.XmlWriter.Create(fileName))
{
XmlSerializer xs =
new XmlSerializer(typeof(List<Contact.Contact>), types.ToArray());
List<Contact.Contact> data = this.ToList();
xs.Serialize(stream, data);
}
}
幽默我:嘗試反序列化到一個數組。 – 2010-07-26 07:37:18
@史蒂文:我很抱歉,我不明白,你能解釋一下嗎? – Gorgen 2010-07-26 07:43:32
@Steven:我想你的意思是我應該搜索「反序列化到數組」。謝謝你提出一個好的搜索。 – Gorgen 2010-07-26 07:52:31