473,758 Members | 2,401 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.Collect ions.Generic.IL ist) 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.GetLis t(). But the only thing that is returned from the web method
call is:

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

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(strin g key, string desc, bool isActive)
{
this._key = key;
this._desc = desc;
this._isActive = isActive;
}

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

[System.Xml.Seri alization.XmlEl ementAttribute( )]
public string Key
{
get
{
return _key;
}
}

[System.Xml.Seri alization.XmlEl ementAttribute( )]
public string Desc
{
get
{
return _desc;
}
}

[System.Xml.Seri alization.XmlEl ementAttribute( )]
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.GetLis t();

return someTypes;
}
Nov 23 '05 #1
3 11759
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.Collect ions.Generic.IL ist) 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.GetLis t(). But the only thing that is returned from the web method
call is:

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

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(strin g key, string desc, bool isActive)
{
this._key = key;
this._desc = desc;
this._isActive = isActive;
}

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

[System.Xml.Seri alization.XmlEl ementAttribute( )]
public string Key
{
get
{
return _key;
}
}

[System.Xml.Seri alization.XmlEl ementAttribute( )]
public string Desc
{
get
{
return _desc;
}
}

[System.Xml.Seri alization.XmlEl ementAttribute( )]
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.GetLis t();

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 XmlTypeAttribut e):
[System.Serializ ableAttribute()]
[System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType = 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(strin g key, string desc, bool isActive)
{
this._key = key;
this._desc = desc;
this._isActive = isActive;
}

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

[System.Xml.Seri alization.XmlEl ementAttribute( )]
public string Key
{
get
{
return _key;
}
set
{
_key = value;
}
}

[System.Xml.Seri alization.XmlEl ementAttribute( )]
public string Desc
{
get
{
return _desc;
}
set
{
_desc = value;
}
}

[System.Xml.Seri alization.XmlEl ementAttribute( )]
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_someTy pe 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">fal se</IsActive>
</_someType>
</ArrayOf_someTyp e>

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.GetDataStr ucture();
XmlTextWriter xtw = new XmlTextWriter(" TestMe.xml",
System.Text.Enc oding.UTF8);
Debug.WriteLine (newCriteria.To String());
criteria.WriteX ml(xtw);
xtw.Flush();
xtw.Close();
XmlTextReader xtr = new XmlTextReader(n ew FileStream("Tes tMe.xml",
FileMode.Open)) ;
DataStructure newCriteria = new DataStructure ();
newCriteria.Rea dXml( xtr );
xtr.Close();
Debug.WriteLine (newCriteria.To String());
"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 XmlTypeAttribut e):
[System.Serializ ableAttribute()]
[System.Xml.Seri alization.XmlTy peAttribute(Ano nymousType = 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(strin g key, string desc, bool isActive)
{
this._key = key;
this._desc = desc;
this._isActive = isActive;
}

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

[System.Xml.Seri alization.XmlEl ementAttribute( )]
public string Key
{
get
{
return _key;
}
set
{
_key = value;
}
}

[System.Xml.Seri alization.XmlEl ementAttribute( )]
public string Desc
{
get
{
return _desc;
}
set
{
_desc = value;
}
}

[System.Xml.Seri alization.XmlEl ementAttribute( )]
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_someTy pe 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">fal se</IsActive>
</_someType>
</ArrayOf_someTyp e>

Nov 23 '05 #4

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

Similar topics

1
1959
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 accept. Please help! Here's the output: <?xml version="1.0" encoding="utf-8"?> <ArrayOfPasswordEntry xmlns:xsd="http://www.w3.org/2001/XMLSchema"
3
4268
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
1034
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
2337
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 having to do a httpwebreqeust call.
6
7217
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
1712
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 having a problem storing a Prop object in a TagType object. The code below is giving me a "Object reference not set to an instance of an object." error. I don't understand why Dim t As TagType = Me.lstTagTypes.SelectedItem 'user selects TagType...
2
8535
by: Umi | last post by:
How can we serialize(xml serialisation) the IList C# object
2
2530
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? (This is not really restricted to returning collections but that's the example I'm going to focus on) For example, let's say you have method that is going to return a "list of books".
0
1175
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 abstract class ProductBase { protected int _id; public int ID
0
9299
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9740
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8744
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7287
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3832
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2702
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.