473,503 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StackOverflowException when serializing

Hi,

I work against .NET 2.0 using C# 2.0. I am facing a problem trying to
serialize the class foo (given below). Basically, foo can contain
other foos in a list. When I try to create a serializer, using the
test() method below, I get a superb StackOverflowException. I googled
for it but didn't found any answer to why I got this exception and how
to serialize foo.

Any help accepted :-)

Thanks,

public void test()
{
XmlSerializer serializer = new XmlSerializer(typeof(foo));
....
}

[Serializable]
public class foo : IEnumerable<foo>
{
// ... some members...
List<foog;

public foo()
{
g = new List<foo>();
}

public IEnumerator<fooGetEnumerator()
{
foreach (foo element in g)
{
yield return element;
}
}

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public void Add(Object e)
{
g.Add(e as foo);
}
}

Nov 5 '07 #1
2 4078
The problem here comes from the fact that you are implementing
IEnumerable<Ton T itself (in this case foo). You are better of creating a
container class which implements IEnumerable<fooand then serialize that.

Also, you don't need to the Serializable attribute when using XML
Serialization.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<et************@fr.michelin.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
Hi,

I work against .NET 2.0 using C# 2.0. I am facing a problem trying to
serialize the class foo (given below). Basically, foo can contain
other foos in a list. When I try to create a serializer, using the
test() method below, I get a superb StackOverflowException. I googled
for it but didn't found any answer to why I got this exception and how
to serialize foo.

Any help accepted :-)

Thanks,

public void test()
{
XmlSerializer serializer = new XmlSerializer(typeof(foo));
...
}

[Serializable]
public class foo : IEnumerable<foo>
{
// ... some members...
List<foog;

public foo()
{
g = new List<foo>();
}

public IEnumerator<fooGetEnumerator()
{
foreach (foo element in g)
{
yield return element;
}
}

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public void Add(Object e)
{
g.Add(e as foo);
}
}

Nov 5 '07 #2
Your foo.g member is recursive. You need an object-reference type of
serialization. The easiest fix for you would be to upgrade to WCF
(.NET 3.0) and use the DataContractSerializer with its support for
object references.

Nov 6 '07 #3

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

Similar topics

5
17244
by: Jesee | last post by:
I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework programming",i came across "Exception handing". Page 405 says "If the stack overflow occurs within the CLR itself,your...
0
1327
by: Elizabeth | last post by:
I have C# dll which I compile using ComInterop True and generate tlb type library. When I call function from the C# dll using below code I get an error "StackOverflowException in mscorlib.dll" ...
11
2871
by: Alx Sharp | last post by:
Hello group... I've created a collection class that implements the following interfaces: IBindingList, IList, ICollection, IEnumerable and ITypedList: abstract class DataCollectionBase :...
20
3344
by: zapov | last post by:
Hi! I'm having some wierd problems with this exception (error). If I use sql commands to insert data into sql server i get strange behaviour from my application. First I used a single...
3
3547
by: Patrick.O.Ige | last post by:
Error:- System.StackOverflowException: Exception of type System.StackOverflowException was thrown. When is this thrown.. Any ideas
8
2472
by: Lars-Erik Aabech | last post by:
Hi! I've got an asp.net page that works for all users except one and that one user only gets the error with a certain parameter set to a certain value. (Same value as the others, but for this...
10
14816
by: Mae Lim | last post by:
Dear all, I'm new to C# WebServices. I compile the WebService project it return no errors "Build: 1 succeeded, 0 failed, 0 skipped". Basically I have 2 WebMethod, when I try to invoke the...
1
2068
by: Thomee Wright | last post by:
I'm having a problem with a pair of applications I'm developing. I have a server application which interacts with several instruments, and a client app which connects to the server and provides a...
9
2480
by: =?Utf-8?B?d2luZHNpbQ==?= | last post by:
Hi, I have a project based on .Net 1.1 and VS 2003,now I am trying to upgrade it to .Net 2.0 and VS 2005.The project passes the 'Build Solution',but When I start Debug, it suddenly comes...
0
7202
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
7278
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
5578
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
5013
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
4672
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
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
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...

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.