473,508 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serializing a collection (IList) in a web service on .NET 2.0

So here's the challenge...

How can a collection (System.Collections.Generic.IList) of some custom type
be serialized in a web service using .NET 2.0?

Below are the class and the web methods in question. The interesting thing
is that at each step of the way, DAL & Biz, the someType class is decorated
with the Serializable attribute. And the someTypes collection in the web
method is correctly populated with the data after the call to
someType.GetList(). But the only thing that is returned from the web method
call is:

<?xml version="1.0" encoding="utf-8" ?>
- <ArrayOf_someType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<_someType />
</ArrayOf_someType>

I've worked through a variety of options but nothing seems to be a feasible
solution. Someone please enlighten me!

Thanks...
Here's the _someType class:
[Serializable]
public sealed class _someType
{
private readonly String _key;
private readonly String _desc;
private readonly Boolean _isActive;

public _someType(string key, string desc, bool isActive)
{
this._key = key;
this._desc = desc;
this._isActive = isActive;
}

public _someType()
{
//nothing...needed to allow to be serialized from
//service facade
}

[System.Xml.Serialization.XmlElementAttribute()]
public string Key
{
get
{
return _key;
}
}

[System.Xml.Serialization.XmlElementAttribute()]
public string Desc
{
get
{
return _desc;
}
}

[System.Xml.Serialization.XmlElementAttribute()]
public bool IsActive
{
get
{
return _isActive;
}
}
}

And here's the web method:
[WebMethod]
public List<_someType> GetAll()
{
//instantiate an instance of SomeType (at the middle tier)
SomeType someType = new SomeType();

//return a collection of _someType

List<_someType> someTypes = someType.GetList();

return someTypes;
}
Nov 23 '05 #1
3 11739
JEB
Try to use the example I just found in the MSDN Link for serializing
dictionaries (collections). Go here to read more:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

JEB

"RandomEngineer" wrote:
So here's the challenge...

How can a collection (System.Collections.Generic.IList) of some custom type
be serialized in a web service using .NET 2.0?

Below are the class and the web methods in question. The interesting thing
is that at each step of the way, DAL & Biz, the someType class is decorated
with the Serializable attribute. And the someTypes collection in the web
method is correctly populated with the data after the call to
someType.GetList(). But the only thing that is returned from the web method
call is:

<?xml version="1.0" encoding="utf-8" ?>
- <ArrayOf_someType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<_someType />
</ArrayOf_someType>

I've worked through a variety of options but nothing seems to be a feasible
solution. Someone please enlighten me!

Thanks...
Here's the _someType class:
[Serializable]
public sealed class _someType
{
private readonly String _key;
private readonly String _desc;
private readonly Boolean _isActive;

public _someType(string key, string desc, bool isActive)
{
this._key = key;
this._desc = desc;
this._isActive = isActive;
}

public _someType()
{
//nothing...needed to allow to be serialized from
//service facade
}

[System.Xml.Serialization.XmlElementAttribute()]
public string Key
{
get
{
return _key;
}
}

[System.Xml.Serialization.XmlElementAttribute()]
public string Desc
{
get
{
return _desc;
}
}

[System.Xml.Serialization.XmlElementAttribute()]
public bool IsActive
{
get
{
return _isActive;
}
}
}

And here's the web method:
[WebMethod]
public List<_someType> GetAll()
{
//instantiate an instance of SomeType (at the middle tier)
SomeType someType = new SomeType();

//return a collection of _someType

List<_someType> someTypes = someType.GetList();

return someTypes;
}

Nov 23 '05 #2
Thanks JEB, that link helped a bit. The new custom type class and the web
method output are listed below. Things are working at the web service level
now, without any issues.

Now the problem is seeing that web method output in a win form. The call the
the web method from the win form works great. The result that is returned has
the correct number of rows but, each row is filled with null (i.e. each
property of the _someType item in the array is null).

It's another serialization issue, but now on the win form client side. Any
suggestions?


Here's the NEW _someType class (simply added the XmlTypeAttribute):
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType = true, Namespace =
"http://tempuri.org/xDS.xsd")]
public sealed class _someType
{
private readonly String _key;
private readonly String _desc;
private readonly Boolean _isActive;

public _someType(string key, string desc, bool isActive)
{
this._key = key;
this._desc = desc;
this._isActive = isActive;
}

public _someType()
{
//nothing...needed to allow to be serialized from
//service facade
}

[System.Xml.Serialization.XmlElementAttribute()]
public string Key
{
get
{
return _key;
}
set
{
_key = value;
}
}

[System.Xml.Serialization.XmlElementAttribute()]
public string Desc
{
get
{
return _desc;
}
set
{
_desc = value;
}
}

[System.Xml.Serialization.XmlElementAttribute()]
public bool IsActive
{
get
{
return _isActive;
}
set
{
_isActive = value;
}
}
}

And here's the output from the WebService call:
<?xml version="1.0" encoding="utf-8" ?>
- <ArrayOf_someType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
- <_someType>
<Key xmlns="http://tempuri.org/xDS.xsd">Random</Code>
<Desc xmlns="http://tempuri.org/xDS.xsd">Random First Test</Description>
<IsActive xmlns="http://tempuri.org/xDS.xsd">false</IsActive>
</_someType>
- <_someType>
<Key xmlns="http://tempuri.org/xDS.xsd">Jack</Code>
<Desc xmlns="http://tempuri.org/xDS.xsd">Jack Test</Description>
<IsActive xmlns="http://tempuri.org/RoleDS.xsd">false</IsActive>
</_someType>
</ArrayOf_someType>

Nov 23 '05 #3
JEB
I can tell you what I did to test my serialization issues. I created a block
of code to write my XML to a file and then read it back up into a new object.
Then, I compared the two objects (visually) to ensure the data was working
before I called it from the web services. Below is the logic I used. Maybe
this will help. In my DataStructure class, I also added a new implementation
of ToString() so I could compare the two. Once this passed, it worked in my
web service.

DataStructure criteria = this.GetDataStructure();
XmlTextWriter xtw = new XmlTextWriter("TestMe.xml",
System.Text.Encoding.UTF8);
Debug.WriteLine(newCriteria.ToString());
criteria.WriteXml(xtw);
xtw.Flush();
xtw.Close();
XmlTextReader xtr = new XmlTextReader(new FileStream("TestMe.xml",
FileMode.Open));
DataStructure newCriteria = new DataStructure ();
newCriteria.ReadXml( xtr );
xtr.Close();
Debug.WriteLine(newCriteria.ToString());
"RandomEngineer" wrote:
Thanks JEB, that link helped a bit. The new custom type class and the web
method output are listed below. Things are working at the web service level
now, without any issues.

Now the problem is seeing that web method output in a win form. The call the
the web method from the win form works great. The result that is returned has
the correct number of rows but, each row is filled with null (i.e. each
property of the _someType item in the array is null).

It's another serialization issue, but now on the win form client side. Any
suggestions?


Here's the NEW _someType class (simply added the XmlTypeAttribute):
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType = true, Namespace =
"http://tempuri.org/xDS.xsd")]
public sealed class _someType
{
private readonly String _key;
private readonly String _desc;
private readonly Boolean _isActive;

public _someType(string key, string desc, bool isActive)
{
this._key = key;
this._desc = desc;
this._isActive = isActive;
}

public _someType()
{
//nothing...needed to allow to be serialized from
//service facade
}

[System.Xml.Serialization.XmlElementAttribute()]
public string Key
{
get
{
return _key;
}
set
{
_key = value;
}
}

[System.Xml.Serialization.XmlElementAttribute()]
public string Desc
{
get
{
return _desc;
}
set
{
_desc = value;
}
}

[System.Xml.Serialization.XmlElementAttribute()]
public bool IsActive
{
get
{
return _isActive;
}
set
{
_isActive = value;
}
}
}

And here's the output from the WebService call:
<?xml version="1.0" encoding="utf-8" ?>
- <ArrayOf_someType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
- <_someType>
<Key xmlns="http://tempuri.org/xDS.xsd">Random</Code>
<Desc xmlns="http://tempuri.org/xDS.xsd">Random First Test</Description>
<IsActive xmlns="http://tempuri.org/xDS.xsd">false</IsActive>
</_someType>
- <_someType>
<Key xmlns="http://tempuri.org/xDS.xsd">Jack</Code>
<Desc xmlns="http://tempuri.org/xDS.xsd">Jack Test</Description>
<IsActive xmlns="http://tempuri.org/RoleDS.xsd">false</IsActive>
</_someType>
</ArrayOf_someType>

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1951
by: Chris Becker | last post by:
How can I change the name of the root element from ArrayOfPasswordEntry to PasswordList or whatever? I have tried before the public class PasswordEntries definition, but XmlSerializer won't...
3
4254
by: hman | last post by:
Hi, I have a collection class where I've implemeneted the ICollection Interface. Here is a small code segment. public class PageList : ICollection, IComparer, IEnumerable, IList {
4
1030
by: Daniel | last post by:
Simple Question. Why this simple code will not compile? Dim obj As Object For Each obj In GroupBox1 Next
6
2308
by: kbs | last post by:
Hi, I'm looking for some good examples that illustrate how to code a web service that exposes a custom collection so that the properties of the collection are accessible on the client without...
6
7191
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
5
1701
by: Bryan | last post by:
I have a class 'TagType' with an ilist member 'Props' that holds a collection of another class called 'Prop'. I let the user create TagTypes and save multiple properties (Props) in them. I am...
2
8528
by: Umi | last post by:
How can we serialize(xml serialisation) the IList C# object
2
2518
by: Veloz | last post by:
Hi there My question is regarding how to best return "collections" from a method call. Should you return an actual object or an interesting/ appropriate interface of the object, to the caller?...
0
1159
by: Shaul | last post by:
Hi, My goal is to serialize a collection of type IList<AbstractClass> which contains derived objects instances. My domain object model is a bit complex so I've created a demmi one: public...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.