.NET can't deserialize nested structures?

I'm having trouble getting C # (VS2008, Compact Framework, .NET version 3.5 SP1) for successfully deserializing nested structures. The problem only occurs in CF, when I run the emulator for a mobile device (I use the emulator "Pocket PC 2003 Second Edition"), the same code that works in my Windows box does not have the same problem.

Here is my code:

public struct Fred
{

    public string Name;
}

public struct Middle
{

    public Fred[] Freds;
}

public struct Top
{

   public Middle Middle;
   public Fred[] Freds;
}

public static void Test()
{

    Top top = new Top();
    top.Middle.Freds = new Fred[2];
    top.Middle.Freds[0].Name = "Fred20";
    top.Middle.Freds[1].Name = "Fred21";
    top.Freds = new Fred[2];
    top.Freds[0].Name = "Fred10";
    top.Freds[1].Name = "Fred11";
    StringBuilder sb = new StringBuilder();
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(top.GetType());

    using (StringWriter sw = new StringWriter(sb))
    {
        x.Serialize(sw, top);
    }

    string xml = sb.ToString();
    string[] lines = xml.Split(new char[] { '\r', '\n' });

    foreach (string line in lines)
    {
        Debug.WriteLine("   " + line.Trim());
    }

    MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(xml));
   StreamReader sr = new StreamReader(ms);
   object o = x.Deserialize(sr);
   Debug.WriteLine("Deserialized into " + o);
   Top go2 = (Top)o;

   if (go2.Freds == null)
        Debug.WriteLine("   go2.Freds is null");
   else
       Debug.WriteLine("   go2.Freds[0].Name is \"" + go2.Freds[0].Name + "\"");

   if (go2.Middle.Freds == null)
       Debug.WriteLine("   go2.Middle.Freds is null");
   else
       Debug.WriteLine("   go2.Middle.Freds[0].Name is \"" + go2.Middle.Freds[0].Name + "\"");
}

When I run this, the generated XML looks good:

<?xml version="1.0" encoding="utf-16"?>
<Top xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Middle>
      <Freds>
         <Fred>
            <Name>Fred20</Name>
         </Fred>
         <Fred>
            <Name>Fred21</Name>
         </Fred>
      </Freds>
   </Middle>
   <Freds>
      <Fred>
         <Name>Fred10</Name>
      </Fred>
      <Fred>
         <Name>Fred11</Name>
      </Fred>
   </Freds>
</Top>

but C # cannot successfully deserialize this XML - console output:

Deserialized into Top
go2.Freds[0].Name is "Fred10"
go2.Middle.Freds is null

xsd has similar problems:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Top" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Top" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
 <xs:choice minOccurs="0" maxOccurs="unbounded">
  <xs:element name="Middle">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Freds" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Fred" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Name" type="xs:string" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
 </xs:element>
</xs:schema>

I just ran into a C # error? Or am I missing something obvious?

. , George, Fred, Middle public George [] George, .

-1
1

TL;DR ( ): .

1: Protobuf. .

2: :

, .

, Compact Framework, /, .NET. - .

Compact Framework, , -, . Protobuf ( 12 , XmlSerializer)

, :

Install-Package protobuf-net

- . , , / . .

, :

1: .

, - " "

[ProtoContract]
public struct Fred
{
    [ProtoMember(1)]
    public string Name;
}

[ProtoContract]
public struct Middle
{
    [ProtoMember(1)]
    public Fred[] Freds;
}

[ProtoContract]
public struct Top
{
    [ProtoMember(1)]
    public Middle Middle;

    [ProtoMember(2)]
    public Fred[] Freds;
}

, , , . , JSON XML, , protobuf . , .

Builder, Top, , :

public class TopTestBuilder
{
    public Top BuildDefaultTestTop()
    {
        var top = new Top
        {
            Middle = new Middle
            {
                Freds = new[]
                {
                    new Fred {Name = "Fred20"},
                    new Fred {Name = "Fred21"}
                }
            },
            Freds = new[]
            {
                new Fred {Name = "Fred10"},
                new Fred {Name = "Fred11"}
            }
        };

        return top;
    }
}

:

Top topIn = new TopTestBuilder().BuildDefaultTestTop();
string serialized;
using (var stream = new MemoryStream())
{
    Protobuf.Serializer.Serialize(stream, topIn);

    stream.Position = 0;
    var reader = new StreamReader(stream);
    serialized = reader.ReadToEnd();
}
// Output: "\nDC4\n\b\nACKFred20\n\b\nACKFred21DC2\b\nACKFred10DC2\b\nACKFred11"

:

Top topOut;
using (var stream = new MemoryStream())
{
    var writer = new StreamWriter(stream);
    writer.Write(serialized);
    writer.Flush();
    stream.Position = 0;

    topOut = Protobuf.Serializer.Deserialize<Top>(stream);
}

, MemoryStreams, , . TypeModel, .

2:

Protobuf TypeModel, ProtoBuf.Meta.RuntimeTypeModel.Default. , Protobuf.Serializer . . ( : RTFM), , , :

var model = TypeModel.Create();

// The first parameter (maps to ProtoContractAttribute) is the Type to be included.
// The second parameter determines whether to apply default behavior,
// based on the attributes. Since we're not using those, this has no effect.
model.Add(typeof(Fred), false);
model.Add(typeof(Middle), false);
model.Add(typeof(Top), false);

// The newly added MetaTypes can be accessed through their respective Type indices.
//   The first parameter is the unique member number, similar to ProtoMemberAttribute.
//   The second parameter is the name of the member as it is declared in the class.
//   When the member is a list:
//     The third parameter is the Type for the items.
//     The fourth parameter is the Type for the list itself.
model[typeof(Fred)].Add(1, "Name");
model[typeof(Middle)].Add(1, "Freds", typeof(Fred), typeof(Fred[]));
model[typeof(Top)].Add(1, "Middle");
model[typeof(Top)].Add(2, "Freds", typeof(Fred), typeof(Fred[]));

, , :

:

Top topIn = new TopTestBuilder().BuildDefaultTestTop();
string serialized;
using (var stream = new MemoryStream())
{
    model.Serialize(stream, top);

    stream.Position = 0;
    var reader = new StreamReader(stream);
    serialized = reader.ReadToEnd();
}

Deserialize:

Top topOut;
using (var stream = new MemoryStream())
{
    var writer = new StreamWriter(stream);
    writer.Write(serialized);
    writer.Flush();
    stream.Position = 0;

    topOut = (Top) _model.Deserialize(stream, null, typeof (Top));
}

. , , - Serialize Deserialize BuildTypeModel ( ?)

:

var serializer = new CustomProtoBufSerializer();

var serialized = serializer.Serialize(someClassInput);

SomeClass someClassOutput = serializer.Deserialize(serialized);

, - Protobuf , JSON XML-. , , , . , , .

, - : Protobuf T4 TypeModel Generator. . , .

, .

+1

Source: https://habr.com/ru/post/1584515/