2010-02-10 2 views
1

저는 Silverlight를 처음 사용했습니다. 나는 주로 직렬화와 비 직렬화에 의존하는 프로젝트를 위해 일하고있다.메시지에서 일치하지 않는 그룹 태그가 발견되었습니다. - protobuf-net

이전에는 WPF의 경우 Serializable 클래스에 익숙했습니다. 실버 라이트에서, 나는 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”  

감사와 비슷하다.
Vinodh

답변

1

(참고 : 나는 "그룹 태그"문제를 재현 할 수 있었다; 지금 제거이 내 첫번째 생각의 편집 기록을 참조, 당신은 제가 감사하게 될 거라고이 재현 도움이 될 수있는 경우)

문제는 SubReports입니다. 이 과 같이 목록 으로 직렬화 엔티티 ([ProtoContract])로 정의했습니다. 후자가 우선하므로 하위 보고서 (항상 null?)를 직렬화하려고했습니다.

당신이로 변경하는 경우 :

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

하거나 완전히 제거하고 Report.SubReportsList<SubReport>이 잘 작동해야 할 경우. 다음 작품 :

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

는 블롭 표시 :

0A040A0251310A040A0251320A040A0251330A040A025134

+0

덕분에 빠른 응답을 많이 마크를. 나는 이것을 조사 할 것이다. –

+0

안녕하세요 Marc, 마지막 답변 자체가 내 질문을 해결했습니다. 사실, XML 파일을 비 직렬화하고 [Serializable] 클래스를 채우려 고했습니다. 우리는 데스크톱 응용 프로그램에서이 클래스를 사용했습니다. 그리고 위의 제안을 사용하여 "하위 보고서"에 "null"이 표시됩니다. 감사합니다. :) –

관련 문제