在Serialize方法处生成XML文档时出错

本文关键字:XML 文档 出错 Serialize 方法 | 更新日期: 2024-09-25 14:10:23

作为这个链接,我有公共属性,尽管这个错误There was an error generating the XML document发生在SaveToXML方法中的ser.Serialize(sw, this);

我把这个类作为父类,它也有一个属性!

public  class XMLCollection<T> where T: new ()
{
   private XmlSerializer ser;
   private  List<T> m_InnerList= new List<T>();
   public List<T> InnerList { 
       get
       { return m_InnerList; }
       set
       { m_InnerList = value; }
   }
   public XMLCollection()
   {
       ser = new XmlSerializer(this.GetType(), new XmlRootAttribute(this.GetType().Name ));
   }
   /// <summary>
   /// 
   /// </summary>
   /// <param name="path">path format: @"D:'FolderName"</param>
   public void SaveToXML(string path)
   {
       try
       {
           using (StreamWriter sw = new StreamWriter(path+ "''"+this.GetType().Name+".xml"))
           {
               ser.Serialize(sw, this);
           }
       }
       catch (Exception ex)
       {
           Console.WriteLine(ex.Message );
       }                   
   }
   public void LoadFromXML(string FullPath)
   {
       try
       {
           if (File.Exists(FullPath))
           {
               using (StreamReader sr= new StreamReader(FullPath))
               {
                   this.InnerList=((XMLCollection<T>)  ser.Deserialize(sr)).InnerList ;
               }
           }
           else
           {
               Console.WriteLine("File not exist....");
           }
       }
       catch (Exception ex)
       {
           Console.WriteLine(ex.Message );
       }      
   }       
}

CollectionList就是从它派生出来的:

public  class CollectionList :XMLCollection<Moshakhase>, IList<Moshakhase>, IDisposable    
{            
   public enum SortType
   {
     Name, ID
   }
   #region ctor
   public CollectionList()
   {
   }
   public CollectionList(Moshakhase item)
   {
       this.Add(item);
   }
   public CollectionList(params Moshakhase[] item)
   {
       InnerList.AddRange(item);
   }
   #endregion
   public override string ToString()
   {
       string str = this.GetType().Name + ":'r'n";
       foreach (Moshakhase item in this)
       {
           str += string.Format("'t{0}({1})'r'n", item.Name, item.Id);
       }
       return str;
   }
   #region Sort
   public void sort(SortType type)
   {
       switch (type)
       {
           case SortType.Name:
               InnerList.Sort();
               break;
           case SortType.ID:
               InnerList.Sort(new IDCompare());
               break;
           default:
               break;
       }
   }
   class IDCompare : IComparer<Moshakhase>
   {
       public int Compare(Moshakhase x, Moshakhase y)
       {
           if (x.Id > y.Id)
           {
               return 1;
           }
           else if (x.Id < y.Id)
           {
               return -1;
           }
           return 0;
       }
   }
   #endregion
   #region Ilist
   public int IndexOf(Moshakhase item)
   {
       return InnerList.IndexOf(item);
   }
   public void Insert(int index, Moshakhase item)
   {
       InnerList.Insert(index, item);
   }
   public void RemoveAt(int index)
   {
       InnerList.RemoveAt(index);
   }
   public Moshakhase this[int index]
   {
       get
       {
           return  InnerList[index];
       }
       set
       {
           InnerList[index] = value;
       }
   }
   public void Add(Moshakhase item)
   {
       InnerList.Add(item);
   }
   public void Clear()
   {
       InnerList.Clear();
   }
   public bool Contains(Moshakhase item)
   {
       return  InnerList.Contains(item);
   }
   public void CopyTo(Moshakhase[] array, int arrayIndex)
   {
       InnerList.CopyTo(array,arrayIndex );
   }
   public int Count
   {
       get { return  InnerList.Count ; }
   }
   public bool IsReadOnly
   {
       get { throw new NotImplementedException(); }
   }
   public bool Remove(Moshakhase item)
   {
       return InnerList.Remove(item);
   }
   public IEnumerator<Moshakhase> GetEnumerator()
   {
       return InnerList.GetEnumerator();
   }
   System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
   {
       return InnerList.GetEnumerator();
   }
   #endregion

   public void Dispose()
   {
       GC.SuppressFinalize(this);
   }   
}

Users来源于CollectionList:

public class Users : CollectionList
{                
    public Users()
    {
    }
    public Users(ketabkhune.unique.User user): base(user)
    {
    }
    public Users(params ketabkhune.unique.User[] user):base(user)
    {
    }
}

我用这个代码来检查它:

Users uList = new Users(new User[] {new User(4,"Kamran"), new User(3,"Sara"), new User(5,"Bahar") });
uList.SaveToXML(@"F:'xml");

在Serialize方法处生成XML文档时出错

当我将XMLCollectionCollectionList组合并将CollectionList定义为generic class时。那个错误消失了。。

不要忘记空白构造函数

[Serializable()]
public  class CollectionList<T> : IList<T>
{
   private XmlSerializer ser;
   private List<T> InnerList = new List<T>();

   #region ctor
   public CollectionList()
   {
       ser = new XmlSerializer(this.GetType());
   }
   public CollectionList(string CollectionName)
   {
       ser = new XmlSerializer(this.GetType(), new XmlRootAttribute(CollectionName ));
   }
....

在线型波纹管处更改代码:

ser.Serialize(sw,m_InnerList);