Hi John,
I think the problem you encountered is caused by the following things:
1. XmlRoot Attribute indicate that this class will be serizlied as the
RootElement of a XML document , however the Collection class is just a sub
element in the webservice SOAP message which can't be validated by the
runtime. So I think XmlRoot attribute can't be applied on your class.
2. As for changing the Serlized Element name of our custom Collection
class, after some local tests, I think we can't modify it directly if our
webmethod return the custom Collection class instance directly as return
type. This is because the ASP.NET webservice geneator will by default treat
such custom Collection class as a Array Type, ( you can verify this by
check the webservice 's webreference/ client proxy in client application),
the client side return Type is an Array of the ArrayItem's class rather
than the Collection class itself. And for Array Type, the .net will always
serilized it as "ArrayOf[ItemTypeName]" .
Currently I haven't found any direct means(Attribute) to modify the output
XML Element of an Array Type if we're using the default serlization of the
..net. However, we can workaround it through a wrapper class which contain
the Custom Collection class as a class member, then we can apply XmlElement
attribute on it to modify it's output ElementName, for example:
#we can specify attribute to customize the Wrapper element and the inner
collection property:
[XmlType("MyWrapper")]
public class Employees
{
private EmployeeCollection _items;
[XmlArray("MyEmployeeColletion")]
public EmployeeCollection Items
{
get
{return _items;}
set
{_items = value;}
}
}
public class EmployeeCollection:ICollection
{
.....
}
public class Employee
{
........
}
===========================
the output will be something like:
<MyWrapper xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<MyEmployeeColletion>
<Employee>
<EmpName>EmpName_0</EmpName>
<EmpID>EmpID_0</EmpID>
</Employee>
<Employee>
<EmpName>EmpName_1</EmpName>
<EmpID>EmpID_1</EmpID>
</Employee>
........................
</MyEmployeeColletion>
</MyWrapper>
=====================================
Thanks,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)