下面的WebService方法返回一个RetObjs类型的对象,它继承了List但增加了一个属性。
public   class   WebService_ReturnObject   :   WebService
{
     public   class   RetObj{}
     public   class   RetObjs   :   List <RetObj>
     {
         private   int   _count;
         public   int   RecordCount
         {
               get   {   return   _count;   }
               set   {   _count   =   value;   }
         }
     }     [WebMethod()]
     [XmlInclude(typeof(RetObjs))]
     public   RetObjs   getRetObjs()
     {
         RetObjs   ret   =   new   RetObjs();
         ret.RecordCount   =   100;         ret.AddRange(new   RetObj[]   {   new   RetObj(),   new   RetObj()   });
         return   ret;
     }
}但我在VS   Studio中通过View   in   Browser执行的结果中没有这个属性,请问各位是什么原因,如何增加上?
    <?xml   version= "1.0 "   encoding= "utf-8 "   ?>  
-   <ArrayOfRetObj   xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "   xmlns:xsd= "http://www.w3.org/2001/XMLSchema "   xmlns= "http://tempuri.org/ ">
    <RetObj   />  
    <RetObj   />  
    </ArrayOfRetObj>

解决方案 »

  1.   

    由于 List<T> 实现了 IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable 这些接口,
    而默认情况下,实现 ICollection 或 IEnumerable 的类 只有集合会被序列化,而公共属性却不会。
      

  2.   

    那怎样加上公共属性呢? 我又换了一种方式,这次有属性了,但是返回的XML多了一层元素 public class ListBase<T>
    {
    public ListBase()
    {
    Records = new List<T>();
    } private int _count;
    [XmlAttribute("RecordCount")]
    public int RecordCount
    {
    get { return _count; }
    set { _count = value; }
    } //[XmlArrayItem(ElementName = "Record", Type = typeof(Employee))]
    //[XmlArray(ElementName = "Records")]
    private List<T> _records;
    public List<T> Records
    {
    get { return _records; }
    set { _records = value; }
    } public void AddRange(T[] array)
    {
    Records.AddRange(array);
    }
    } public class Employees2 : ListBase<Employee>
    {
    } [WebMethod()]
    public Employees2 getRetObjs2()
    {
    Employees2 ret = new Employees2();
    ret.RecordCount = 2; ret.AddRange(new Employee[] { new Employee(), new Employee() });
    return ret;
    }返回:
      <?xml version="1.0" encoding="utf-8" ?> 
    - <Employees2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" RecordCount="2" xmlns="http://tempuri.org/">
    - <Records>
      <Employee /> 
      <Employee /> 
      </Records>
      </Employees2>如何去掉Records,让Employees2下面直接就是Employee,同时还保留RecordCount这个公共属性?
      

  3.   

    呵,默认就是这样了。自己重写了,例如        void WriteXml(System.Xml.XmlWriter writer)
            {
                writer.WriteStartElement("Attributes");
                writer.WriteAttributeString("RecordCount", _count.ToString());
                writer.WriteAttributeString("Other", "value");
                writer.WriteEndElement();            XmlSerializer xmlser = new XmlSerializer(typeof(RetObj), "http://myserver/mynamespace");            for (int i = 0; i < Count; i++) xmlser.Serialize(writer, this[i]);
            }结果(加如 RetObj 有 Age 属性,且默认行为):
    <?xml version="1.0" encoding="utf-8"?>
    <RetObjs xmlns="http://tempuri.org/">
      <Attributes RecordCount="100" Other="value" />
      <RetObj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://myserver/mynamespace">
        <Age>0</Age>
      </RetObj>
      <RetObj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://myserver/mynamespace">
        <Age>0</Age>
      </RetObj>
    </RetObjs>