简体   繁体   English

XML到C#类的StackOverflowException

[英]StackOverflowException in XML to C# class

I am trying to make a class in C# on the basis of the following XML code: 我试图在以下XML代码的基础上在C#中创建一个类:

<?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. 我已经看过这个线程XML到c#问题 ,但我还没能解决问题。 My c# code looks like this: 我的c#代码如下所示:

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 CADtoXML.exe中发生了未处理的“System.StackOverflowException”类型异常

I have tried to use the XML serializer with no luck; 我试图使用XML序列化器没有运气; I think it has something to do with the fact that there is at sub sub element in the XML code. 我认为它与XML代码中存在sub子元素的事实有关。 Book -> Title -> TitleA. 书 - >标题 - > TitleA。 Any help will be greatly appreciated. 任何帮助将不胜感激。

Update 1: 更新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. 然后我将它们导入列表并使用Serializer创建一个新的XML文件。

Don't know if this can help. 不知道这是否有帮助。

Update 2: 更新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: 我不能成为book1.Title.TitleA,然后我必须这样做:

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: 但现在它们是两个不同的对象,book1和book2 ....它们基于两个不同的类,因此我不能使用它(列出对象,然后使它成为XML代码:

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. 我想这样做,所以我得到我的XML代码,子子元素,就像我在第一篇文章中提到的那样。

Thanks for the help so far ;) 感谢你目前的帮助 ;)

You're getting a StackOverflowException because your property is accessing itself, which results in some kind of endless recursion: 您正在获取StackOverflowException因为您的属性正在访问自身,这会导致某种无限递归:
The Book property calls the Book property calls the Book property calls the Book property ... Book属性调用Book属性调用Book属性调用Book属性...

You should use a backing field, or use automatic properties instead: 您应该使用支持字段,或使用自动属性:

public class Catalog
{
    public BookClass Book
    { 
      get; 
      set; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM