WebService的返回值中少了一个int型的属性,向大家请教。

shi_hang_nk 2007-11-21 11:04:04
下面的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>
...全文
154 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
r_swordsman 2007-11-23
  • 打赏
  • 举报
回复
呵,默认就是这样了。自己重写了,例如

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>
shi_hang_nk 2007-11-22
  • 打赏
  • 举报
回复
那怎样加上公共属性呢?

我又换了一种方式,这次有属性了,但是返回的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这个公共属性?
r_swordsman 2007-11-21
  • 打赏
  • 举报
回复
由于 List<T> 实现了 IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable 这些接口,
而默认情况下,实现 ICollection 或 IEnumerable 的类 只有集合会被序列化,而公共属性却不会。


110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧