English 中文(简体)
Mismatched group tags detected in message - protobuf-net
原标题:

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

最佳回答

(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

问题回答

暂无回答




相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

WCF Problem Sending Object To Client

Background Converting from using .Net Remoting to WCF. Most of the methods on the WCF server are working fine, but ran into one that isn t working today. This is the service contract: [...

WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

deserialize the XML Document --- Need Help

I am using the below code snippet now to deserialize the XML document ... [WebMethod] public XmlDocument OrderDocument(XmlDocument xmlDoc) { XmlSerializer serializer = new XmlSerializer(typeof(...

How do I serialize a child class?

how do I include the serialized data from a child class where both impliment iserializeable? Class A Implements ISerializable dim _B as new B Class B Implements ISerializable ...

WCF: Serialize complex objects with read-only members

Looking for some guidance on a WCF service I’m prototyping. I have a WCF service hosted in IIS that will pass data to my clients. I have a separate shared assembly that contains all my business ...

Long/multiple SQL queries vs Serialization

I m trying to improve the performance of my web app where a page does a long query to pull data from different tables on a database. It pulls invoice data with multiple item lines, invoice status, and ...

热门标签