【发布时间】:2014-05-29 10:14:58
【问题描述】:
我正在尝试序列化通用列表,在 XML 序列化器中使用通用列表对象时出现错误,
我的通用列表类是,
class CustomContact
{
private string[] number = new string[5];
public string Name { get; set; }
//public string Number { get; set; }
public string[] Number
{
get { return number; }
set { number = value; }
}
// public string Number1 { get; set; }
public CustomContact()
{
}
//CTOR that takes in a Contact object and extract the two fields we need (can add more fields)
public CustomContact( Contact contact)
{
Name = contact.DisplayName;
int count = contact.PhoneNumbers.Count();
for (int i = 0; i < count; i++)
{
if (count > 0 && contact.PhoneNumbers.ElementAt(i).PhoneNumber != null && !string.IsNullOrEmpty(contact.PhoneNumbers.ElementAt(i).PhoneNumber))
{
Number[i] = contact.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
}
else
{
Number[i] = "";
}
}
/*var number = contact.PhoneNumbers.FirstOrDefault();
if (number != null)
Number = number.PhoneNumber;
else
Number = "";*/
}
通用列表的对象,
List<CustomContact> listOfContacts = new List<CustomContact>();
在 xml 序列化程序中使用此通用列表类的对象,
XmlSerializer serializer = new XmlSerializer(typeof(List<CustomContact>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
//CHANGE "App.MyStorage.Items" to where your data is.
// serializer.Serialize(xmlWriter, App.con.);
serializer.Serialize(xmlWriter, listOfContacts);
}
在这一行出现错误,
XmlSerializer serializer = new XmlSerializer(typeof(List<CustomContact>));
错误是,
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll
字符串类型的列表工作正常如何在此处运行通用列表?
希望您的建议谢谢
完全错误:
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll
Additional information: Could not load file or assembly 'System.Xml.Serialization.debug.resources, Version=4.0.0.0, Culture=en-US, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
【问题讨论】:
-
FileNotFoundException 表明问题出在文件创建上,即
stream的来源;你确定你没有找错地方吗?是哪一行出错了? -
我已经提到了错误的行
-
我在找对地方
-
在您的代码中,
class CustomContact不是public(XmlSerializer需要)-我知道错误是切线的,但是如果您将课程设为public,它会起作用吗? -
@user3664724
class CustomContact不定义了public类型;如果它是顶级的,则为internal,如果它是嵌套的,则为private;XmlSerializer需要public。
标签: c# asp.net .net xml-serialization generic-list