By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.