消息中检测到不匹配的组标签 - protobuf-net

发布于 2024-08-21 02:39:25 字数 1186 浏览 2 评论 0原文

我对 Silverlight 还很陌生。我正在从事一个主要依赖于序列化和反序列化的项目。

以前,对于 WPF,我对 Serialized 类很满意。对于 silverlight,我发现 protobuf 非常有用。但是,我对这个例外感到困扰。我不知道是什么原因导致这个问题。请帮帮我。

我正在使用 Silverlight 3.0。 protobuf-net r282

请找到我正在使用的代码。

[ProtoContract]
public class Report
{
    public Report()
    {
    }

    [ProtoMember(1)]
    public SubReports SubReports { get; set; }
}

[ProtoContract]
public class SubReports
   : List<SubReport>
{
    public SubReports()
    {
    }

    [ProtoMember(1)]
    public SubReport SubReport { get; set; }
}

[ProtoContract]
public class SubReport
{
    public SubReport()
    {
    }

    [ProtoMember(1)]
    public string Name { get; set; }
}

我用来反序列化的代码是

    public static T Deserialize<T>(Byte[] bytes) where T
        : Report
    {
        return ProtoBuf.Serializer.Deserialize<T>(new MemoryStream(bytes));
    }

我的示例 XML,它看起来类似于

Report  
   ...SubReports  
      ...SubReport Name=”Q1 Report”   
      ...SubReport Name=”Q2 Report”   
      ...SubReport Name=”Q3 Report”   
      ...SubReport Name=”Q4 Report”     

提前感谢。
维诺德

I’m quite new to Silverlight. I’m working for a project which mainly depends on Serialization and Deserialization.

Formerly, for WPF I was comfortable with Serializable classes. For silverlight, I found protobuf would be quite useful. But, I'm troubled with this exception. I don't know what causes this problem. Please help me out.

I'm using Silverlight 3.0.
protobuf-net r282

Please find the code which I’m using.

[ProtoContract]
public class Report
{
    public Report()
    {
    }

    [ProtoMember(1)]
    public SubReports SubReports { get; set; }
}

[ProtoContract]
public class SubReports
   : List<SubReport>
{
    public SubReports()
    {
    }

    [ProtoMember(1)]
    public SubReport SubReport { get; set; }
}

[ProtoContract]
public class SubReport
{
    public SubReport()
    {
    }

    [ProtoMember(1)]
    public string Name { get; set; }
}

The Code I’m using to de-serialize is

    public static T Deserialize<T>(Byte[] bytes) where T
        : Report
    {
        return ProtoBuf.Serializer.Deserialize<T>(new MemoryStream(bytes));
    }

My sample XML looks similar to

Report  
   ...SubReports  
      ...SubReport Name=”Q1 Report”   
      ...SubReport Name=”Q2 Report”   
      ...SubReport Name=”Q3 Report”   
      ...SubReport Name=”Q4 Report”     

Thanks in advance.
Vinodh

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

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

发布评论

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

评论(1

赠我空喜 2024-08-28 02:39:25

(注意:我无法重现“组标签”问题;请参阅编辑历史记录以了解我对此的第一个想法,现已删除;如果您能帮助我重现此问题,我将不胜感激)

问题是 SubReports.您已将这两者定义为列表定义为序列化实体([ProtoContract]);后者优先,因此它尝试序列化列表中的单个子报告(始终为null?)。

如果您将其更改为:

// note no attributes, no child property
public class SubReports : List<SubReport> { }

或者如果您完全删除它并使 Report.SubReports 成为 List 它应该可以正常工作。以下工作:

static void Main() {
    byte[] blob;
    // store a report
    using (MemoryStream ms = new MemoryStream()) {
        Report report = new Report {
            SubReports = new List<SubReport> {
                new SubReport { Name="Q1"}, 
                new SubReport { Name="Q2"},
                new SubReport { Name="Q3"},
                new SubReport { Name="Q4"},
            }
        };

        Serializer.Serialize(ms, report);
        blob = ms.ToArray();
    }
    // show the hex
    foreach (byte b in blob) { Console.Write(b.ToString("X2")); }
    Console.WriteLine();

    // reload it
    using (MemoryStream ms = new MemoryStream(blob)) {
        Report report = Serializer.Deserialize<Report>(ms);
        foreach (SubReport sub in report.SubReports) {
            Console.WriteLine(sub.Name);
        }
    }
}

显示 blob:

0A040A0251310A040A0251320A040A0251330A040A025134

(note: I couldn't reproduce the "group tags" issue; see edit history for my first thoughts on this, now removed; if you can help me reproduce this I'd be grateful)

The problem is SubReports. You have defined this both as a list and as a serialization entity ([ProtoContract]); the latter takes precedence, so it was trying to serialize the single sub-report on the list (which is always null?).

If you change this to:

// note no attributes, no child property
public class SubReports : List<SubReport> { }

or if you remove it completely and make Report.SubReports a List<SubReport> it should work fine. The following works:

static void Main() {
    byte[] blob;
    // store a report
    using (MemoryStream ms = new MemoryStream()) {
        Report report = new Report {
            SubReports = new List<SubReport> {
                new SubReport { Name="Q1"}, 
                new SubReport { Name="Q2"},
                new SubReport { Name="Q3"},
                new SubReport { Name="Q4"},
            }
        };

        Serializer.Serialize(ms, report);
        blob = ms.ToArray();
    }
    // show the hex
    foreach (byte b in blob) { Console.Write(b.ToString("X2")); }
    Console.WriteLine();

    // reload it
    using (MemoryStream ms = new MemoryStream(blob)) {
        Report report = Serializer.Deserialize<Report>(ms);
        foreach (SubReport sub in report.SubReports) {
            Console.WriteLine(sub.Name);
        }
    }
}

Displaying the blob:

0A040A0251310A040A0251320A040A0251330A040A025134

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