问题 xml 反序列化递归嵌套对象

发布于 2024-09-11 09:52:04 字数 1762 浏览 3 评论 0原文

已解决:下面的代码不会像我想象的那样导致无限循环。循环位于调用反序列化的代码中。这个发布的代码工作得很好

我正在尝试将以下对象序列化和反序列化为 xml

public class MessageObjectCollection : List<MessageObject>
{
    public string Serialize()
    {
        return XmlObjectSerializer.SerializeObject(this);
    }

    public static MessageObjectCollection DeSerialize(string serializedPriceHistory)
    {
        return XmlObjectSerializer.DeserializeObject<MessageObjectCollection>(serializedPriceHistory);
    }
}

MessageObject 类看起来像这样

public class MessageObject
{
    public string Title;
    public MessageObjectCollection Responses;
}

所以如果我有一个如下所示的 messageobjectcollection 实例:

var msgColl = new MessageObjectCollection
    {
         new MessageObject
              {
                   Title = "Hello",
                   Responses = new MessageObjectCollection
                        {
                             new MessageObject
                                  {
                                      Title = "hi",
                                      Responses = null
                                  }
                        }
              }
    }

我可以通过调用来序列化它 var xml = msgColl.Serialize();

但是,当我尝试通过调用反序列化它时 var msgColl = new MessageObjectCollection().Deserialize(xml);

我在反序列化方法中遇到堆栈溢出异常:

public static T DeserializeObject<T>(string xml)
{
    T result;
    var ser = new XmlSerializer(typeof(T));
    var buffer = StringToUTF8ByteArray(xml);
    using (var stream = new MemoryStream(buffer, 0, buffer.Length))
    {
        result = (T) ser.Deserialize(stream);
    }
    return result;
}

有人知道我做错了什么吗?

Solved: code below is not causing an infinite loop as I thought. the loop was in the code calling the deserialization. this posted code works just fine

I am trying to serialize and deserialize to xml the following object

public class MessageObjectCollection : List<MessageObject>
{
    public string Serialize()
    {
        return XmlObjectSerializer.SerializeObject(this);
    }

    public static MessageObjectCollection DeSerialize(string serializedPriceHistory)
    {
        return XmlObjectSerializer.DeserializeObject<MessageObjectCollection>(serializedPriceHistory);
    }
}

The MessageObject class looks like this

public class MessageObject
{
    public string Title;
    public MessageObjectCollection Responses;
}

So if I have a instance of messageobjectcollection that looks like:

var msgColl = new MessageObjectCollection
    {
         new MessageObject
              {
                   Title = "Hello",
                   Responses = new MessageObjectCollection
                        {
                             new MessageObject
                                  {
                                      Title = "hi",
                                      Responses = null
                                  }
                        }
              }
    }

I can serialize this just fine by calling
var xml = msgColl.Serialize();

However when I try to deserialize this by calling
var msgColl = new MessageObjectCollection().Deserialize(xml);

I get an stack overflow exception in the deserialization method:

public static T DeserializeObject<T>(string xml)
{
    T result;
    var ser = new XmlSerializer(typeof(T));
    var buffer = StringToUTF8ByteArray(xml);
    using (var stream = new MemoryStream(buffer, 0, buffer.Length))
    {
        result = (T) ser.Deserialize(stream);
    }
    return result;
}

Anyone know what I'm doing wrong?

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

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

发布评论

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

评论(1

三寸金莲 2024-09-18 09:52:04

我不确定它是否与问题相关,但由于反序列化方法是静态的,您不应该调用...

var msgColl = MessageObjectCollection.Deserialize(xml);

而不是...

var msgColl = new MessageObjectCollection().Deserialize(xml);

??

Im not sure if its relevant to the problem but as the Deserialize method is static shouldn't you be calling...

var msgColl = MessageObjectCollection.Deserialize(xml);

instead of...

var msgColl = new MessageObjectCollection().Deserialize(xml);

??

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