XML 到 C# 类中的 StackOverflowException

发布于 2024-10-20 08:19:38 字数 2520 浏览 2 评论 0原文

我正在尝试基于以下 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>

我查看了此线程 XML 到 c# 的问题,但我一直没能解决这个问题。我的 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; } }
}

我收到以下错误消息:

CADtoXML.exe 中发生类型为“System.StackOverflowException”的未处理异常

我尝试使用 XML 序列化程序,但没有成功;我认为这与 XML 代码中存在 at sub 子元素有关。书->标题->标题A.任何帮助将不胜感激。

更新 1:

我之前尝试过此解决方案,但后来出现此错误:对象引用未设置到对象的实例。我在主类中运行的代码如下

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

,之后我将它们导入到列表中并使用序列化器创建一个新的 XML 文件。

不知道这是否有帮助。

更新2:

像这样:

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

我仍然有同样的问题。我无法制作 book1.Title.TitleA,那么我必须这样做:

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

但现在它们是两个不同的对象,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);   

我想这样做,因此我得到了带有 sub 子元素的 XML 代码,如我的第一篇文章中所示,

感谢迄今为止的帮助;)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-10-27 08:19:38

您收到 StackOverflowException 是因为您的属性正在访问自身,这会导致某种无休止的递归:
Book 属性调用 Book 属性调用 Book 属性调用 Book 属性...

您应该使用支持字段,或使用自动属性:

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

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; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文