序列化类型问题
using System.Xml.Serialization;
using System.Collections;
namespace CommonLibrary.Email {
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.sunny.com ")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.sunny.com ", IsNullable = false)]
public class MailAccounts {
private MailAccount[] accountField;
[System.Xml.Serialization.XmlElementAttribute( "Account ")]
public MailAccount[] Account
{
get
{
return this.accountField;
}
set
{
this.accountField = value;
}
}
//[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = false)]
private MailCollections accounts = new MailCollections();
[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = true)]
public MailCollections Accounts
{
get { return this.accounts; }
set { this.accounts = value; }
}
}
public class MailCollections : CollectionBase
{
public MailAccount this[int inx]
{
get
{
return ((MailAccount)(base.InnerList[inx]));
}
set
{
base.InnerList[inx] = value;
}
}
public MailAccount Find(string userName)
{
foreach (MailAccount acc in this)
{
if (string.Compare(acc.user, userName, true) == 0)
{
return acc;
}
}
return null;
}
public void AddAccount(MailAccount account)
{
base.InnerList.Add(account);
}
public void RemoveAccount(string userName)
{
base.InnerList.Remove(this.Find(userName));
}
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.sunny.com ")]
public class MailAccount :System.ICloneable{
private string userField;
private string passwordField;
private string popServerField;
private string popPortField;
object System.ICloneable.Clone()
{
return this.Clone();
}
public virtual MailAccount Clone()
{
MailAccount m = this.MemberwiseClone() as MailAccount;
return m;
}
public string user {
get {
return this.userField;
}
set {
this.userField = value;
}
}
public string password {
get {
return this.passwordField;
}
set {
this.passwordField = value;
}
}
public string popServer {
get {
return this.popServerField;
}
set {
this.popServerField = value;
}
}
public string popPort {
get {
return this.popPortField;
}
set {
this.popPortField = value;
}
}
}
}
以上是序列化用的class
在MailAccounts类里如果去掉
[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = true)]
public MailCollections Accounts
{
get { return this.accounts; }
set { this.accounts = value; }
}
然后
XmlSerializer machineSerializer = new XmlSerializer(typeof(CommonLibrary.Email.MailAccounts));
正确
如果不去掉
就会throw异常
There was an error reflecting type 'CommonLibrary.Email.MailAccounts '
请教那个MailCollection什么地方有问题
或者说在public MailCollections Accounts 这上面加什么属性
[解决办法]
把
//[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = false)]
private MailCollections accounts = new MailCollections();
改成
//[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = true)]
private MailCollections accounts = new MailCollections();
或
//[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = false)]
public MailCollections accounts = new MailCollections();
试试