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

时间:2013-12-13 14:57:06

标签: c# xml inheritance xmlserializer

this link,我有公共属性,但There was an error generating the XML document方法ser.Serialize(sw, this);出现此错误SaveToXML

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

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

2 个答案:

答案 0 :(得分: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 ));
   }

....

答案 1 :(得分:0)

改变你的代码在线像波纹管一样:

ser.Serialize(sw,m_InnerList);