Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackOverflowException in XML to C# class

I am trying to make a class in C# on the basis of the following XML code:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog>
<Book>
    <Title><TitleA>ORK</TitleA></Title>
    <Author>J.D. Salinger</Author>
    <Publisher>Little Brown and Company</Publisher>
    <Pub_Date>1951</Pub_Date>
</Book>
<Book>
    <Title><TitleA>NAA</TitleA></Title>
    <Author>Jan</Author>
    <Publisher>Jans forlag</Publisher>
    <Pub_Date>2011</Pub_Date>
</Book> 
</Catalog>

I have looked on this thread XML to c# Question, but I have not been able to solve the problem. My c# code looks like this:

public class Catalog
{
    public BookClass Book { get { return Book; } set { Book = value; } }
}

public class BookClass
{
    public TitleClass Title { get { return Title; } set { Title = value; } }
    public string Author { get { return Author; } set { Author = value; } }
    public string Publisher { get { return Publisher; } set { Publisher = value; } }
    public string Pub_Date { get { return Pub_Date; } set { Pub_Date = value; } }

}
public class TitleClass
{

    public string TitleA { get { return TitleA; } set { TitleA = value; } }
}

I get the following error message:

An unhandled exception of type 'System.StackOverflowException' occurred in CADtoXML.exe

I have tried to use the XML serializer with no luck; I think it has something to do with the fact that there is at sub sub element in the XML code. Book -> Title -> TitleA. Any help will be greatly appreciated.

Update 1:

I have tried this solution before, but then i get this error: Object reference not set to an instance of an object. The code i am running in the main class is the following

Catalog book1 = new Catalog();
book1.Book.Author = "A";
book1.Book.Publisher = "A";
book1.Book.Pub_Date = "A";

And after this I import them to a list and use the Serializer to make a new XML file.

Don't know if this can help.

Update 2:

Like this:

BookClass book1 = new BookClass();
book1.Author = "A";
book1.Publisher = "A";
book1.Pub_Date = "A";
book1.Title.TitleA = "A";

I still have the same problem. I can't make the book1.Title.TitleA, then I have to do this:

TitleClass book2 = new TitleClass();
book2.TitleA = "A";

But now they are two different objects, book1 and book2.... And they are based on two different classes, and therefore I cannot use this (list the object and afterwards make it an XML code:

List<BookClass, TitleClass> books = new List<BookClass, TitleClass>() { book1, book2 };
XmlSerializer x = new XmlSerializer(typeof(List<BookClass, TitleClass>), new XmlRootAttribute("TEST"));
x.Serialize(Console.Out, books);   

I want to do that, so I get my XML code, with the sub sub element, like presented in my first post.

Thanks for the help so far ;)

like image 972
Thomas Avatar asked Mar 02 '11 14:03

Thomas


People also ask

Can we handle StackOverflowException?

NET Framework 2.0, you can't catch a StackOverflowException object with a try / catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.

What causes StackOverflowException?

StackOverflowError is a runtime error which points to serious problems that cannot be caught by an application. The java. lang. StackOverflowError indicates that the application stack is exhausted and is usually caused by deep or infinite recursion.

How do I fix stack overflow exception?

Setting the size of the stack or the maximum depth value of the recursion is allowed in most of the programming languages. Now that we have an opportunity to set the value of the depth of the stack, we have to set to a value as small as possible and observe the output.

What is stack overflow error C#?

A StackOverflowException is thrown when the execution stack overflows because it contains too many nested method calls. For example, suppose you have an app as follows: C# Copy. using System; namespace temp { class Program { static void Main(string[] args) { Main(args); // Oops, this recursion won't stop. } } }


1 Answers

You're getting a StackOverflowException because your property is accessing itself, which results in some kind of endless recursion:
The Book property calls the Book property calls the Book property calls the Book property ...

You should use a backing field, or use automatic properties instead:

public class Catalog
{
    public BookClass Book
    { 
      get; 
      set; 
    }
}
like image 122
Frederik Gheysels Avatar answered Oct 03 '22 16:10

Frederik Gheysels