serialization - C# Compact Framework - OutOfMemoryException with XmlSerializer.Serialize -
I am trying to sort a large collection of objects (20,000) objects within the archive. I am doing the following using the following code:
XmlSerializer xs = New XmlSerializer (deserialized.GetType ()); Stringwriters swing; (Using sw = new stringwriter ()) {xs.Serialize (sw, deserialized); // OutOfMemoryException here} string packet = sw.ToString (); Return packet; Is there a better way to do this, or am I doing something wrong?
It seems that should work, but there are unexpected limitations in the CF .
Is there a requirement XML? I do not remember trying to do with 20k records, but there may be another option Try using a different serializer - for example, works on CF2. I can not do this work, but it may be worth a shot.
(In particular, I should not be affected by a very complex object model again to try to do some extra work inside CF currently).
Showing the example; Note that this example works well for XmlSerializer , but Protobf-Net only uses 20% of the space (or if you believe that the character has two bytes in memory each):
using the system; Using System.Collections.Generic; Using System.IO; Using System.Xml.Serialization; Using ProtoBuff; [Serialable, proto contract] public class department {[proto member (1)) public string name {get; Set; } [Proto member (2)] Public list & lt; Person & gt; People {receive; Set; }} [Serializable, proto contract] public class person [[Protomember (1)] Public Entity ID (Received); Set; } [Pronomember (2)] Public string name {get; Set; } [Proto Member (3)] Public Date Time Offfall {Received; Set; }} Fixed class program {[MTAThread] static zero main () {section of department = new section {name = "foo"}; Department People = new list & lt; Person & gt; (); Random rand = new random (123456); For (int i = 0; i <20000; i ++) {person person = new person (); Person.Id = rand.Next (50000); Person.DateOfBirth = DateTime.Today.AddDays (-Rand.Next (2000)); person. Name = "definite name"; Dept.People.Add (person); } Byte [] raw; (MemoryStream MS = new memorystream ()) using {Serializer.Serialize (MS, Department); Raw = ms toArray (); // 473,399 bytes) XmlSerializer ser = New XmlSerializer (type (department)); Stringwright SW = new stringwriter (); Serialize (sw, department); String s = sw.ToString (); // 2,115,693 characters}} I need more information if I need more help - I can talk about this topic all day; Note that this is the only standard XML attributes ( [XmlElement (order = 1)] - I use more specific [ProtoMember (1)] etc. for clarity It allows the precise control of the serialization (biosphere versus two content, versus long-prefix, etc.).
Comments
Post a Comment