StackOverflowException in XML to C# class

2019-06-21 02:50发布

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 ;)

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-21 03:34

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; 
    }
}
查看更多
登录 后发表回答