2013-12-13 43 views
0

作爲this link生成XML文檔時出現錯誤,我有公共屬性,雖然此錯誤There was an error generating the XML document發生在方法ser.Serialize(sw, this);在Serialize方法

我有這門課作爲家長,它也有一個屬性!

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); 
    } 
} 

UsersCollectionList得出:

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"); 

回答

1

當我結合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)); 
    } 

.... 
0

改變你的代碼,其中行像貝婁:

ser.Serialize(SW,m_InnerList);