StackOverflowException在XML到C#类 [英] StackOverflowException in XML to C# class

查看:140
本文介绍了StackOverflowException在XML到C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C#类以下XML代码的基础上:

 < XML版本=1.0编码=ISO-8859-1>?; 
<目录和GT;
<图书>
<标题>< TitleA>&ORK LT; / TitleA>< /标题>
<作者> J.D。塞林格< /作者>
<发布者GT;小布朗公司和LT&; /发行商及GT;
<&PUB_DATE GT; 1951年< / PUB_DATE>
< /书籍及GT;
<图书>
<标题>< TitleA> NAA< / TitleA>< /标题>
<作者> 1月及LT; /作者>
<发布者GT; JANS forlag< /发行商及GT;
<&PUB_DATE GT; 2011< / PUB_DATE>
< /书籍及GT;
< /目录>



我已经看过这个主题的 XML到C#问题,但我一直没能解决的问题。我的C#代码如下所示:

 公共类目录
{
公共BookClass书{{返回书; } {设置书=价值; }}
}

公共类BookClass
{
公共TitleClass名称{{返回名称; } {设置标题=值; }}
公共字符串作者{{返回作者; }集合{作者=价值; }}
公共字符串出版商{{返回出版商; }集合{出版商=价值; }}
公共字符串PUB_DATE {{返回PUB_DATE; }集合{PUB_DATE =价值; }}

}
公共类TitleClass
{

公共字符串TitleA {{返回TitleA; }集合{TitleA =价值; }}
}



我收到以下错误信息:




类型'System.StackOverflowException的未处理的异常出现在CADtoXML.exe




我曾尝试使用没有运气XML序列;我认为这事做的事实,那就是在XML代码子子元素。书 - >标题 - > TitleA。 。任何帮助将不胜感激。



更新1:



我以前也试过这种解决方案,但后来我得到这个错误:对象引用不设置到对象的实例。我在主类中运行的代码如下

 目录BOOK1 =新目录(); 
book1.Book.Author =A;
book1.Book.Publisher =A;
book1.Book.Pub_Date =A;

和在此之后我导入到一个列表,然后使用串行做一个新的XML文件。< 。/ p>

不知道这是否可以帮助



更新2:



这样的:

  BookClass BOOK1 =新BookClass(); 
book1.Author =A;
book1.Publisher =A;
book1.Pub_Date =A;
book1.Title.TitleA =A;



我仍然有同样的问题。我不能让book1.Title.TitleA,那么我必须这样做:

  TitleClass BOOK2 =新TitleClass() ; 
book2.TitleA =A;



但现在他们是两个不同的对象,BOOK1和Book ....他们是基于两个不同的类,所以我不能用这个(列表中的对象,之后使之成为XML代码:

 列表< BookClass,TitleClass> ;书=新的List< BookClass,TitleClass>(){BOOK1,BOOK2}; 
XmlSerializer的X =新的XmlSerializer(typeof运算(列表< BookClass,TitleClass>),新XmlRootAttribute(TEST));
x.Serialize(Console.Out,书籍);

我要做到这一点,所以我得到。我的XML代码,与子子元素,就像在我的第一篇文章介绍



感谢您的帮助,到目前为止;)

解决方案

您会得到一个 StackOverflowException ,因为你的财产正在访问本身,这会导致某种无限递归的:结果
书本属性呼吁书呼吁物业书本属性调用书本属性...



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

 公共类目录
{
公共BookClass书
{
搞定;
组;
}
}


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

解决方案

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

这篇关于StackOverflowException在XML到C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆